restlet: Using Server and Client restlet Jars in same web application - java

I am not able to use restlet server and client jars in same java web application. The problem is some jars of server and client have the same name. If I try to remove duplicate jars I get errors like
java.lang.NoSuchMethodError: org.restlet.Context.getClientDispatcher()Lorg/restlet/Restlet;
org.restlet.resource.ClientResource.createNext(ClientResource.java:503)
org.restlet.resource.ClientResource.getNext(ClientResource.java:829)
org.restlet.resource.ClientResource.handleOutbound(ClientResource.java:1221)
org.restlet.resource.ClientResource.handle(ClientResource.java:1068)
org.restlet.resource.ClientResource.handle(ClientResource.java:1044)
org.restlet.resource.ClientResource.post(ClientResource.java:1453)
com.xxxxxx.web.restletclient.services.CommonService.sendRequest(CommonService.java:25)
com.xxxxxx.web.restletclient.services.adminService.execute(adminService.java:24)
com.xxxxxx.web.restletclient.client.adminLoginClient.connect(AdminLoginClient.java:41)
com.xxxxxx.web.action.operator.adminLoginAction.performAction(adminLoginAction.java:75)
com.xxxxxx.common.action.AbstractBaseAction.execute(AbstractBaseAction.java:137)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:754)
javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
My scenario is such that my web application can work as a web service client as well as server. So I am looking for an option where I can use restlet client and server jars in same web application. I have searched over the net, but did not find any solution yet which will work.
Thank you for your help.

In fact, the core jar of Restlet supports both client and server sides. That said extensions (jar files with the name org.restlet.extension.XXX) can be specified to one or other side or both. It depends on the extension.
Could you give me the list of jars we tried to use?
Here is a sample pom file you could use to initialize the dependencies of your Restlet project:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.restlet</groupId>
<artifactId>restlet-war</artifactId>
<name>${project.artifactId}</name>
<packaging>war</packaging>
<version>1.0.0-snapshot</version>
<properties>
<java-version>1.7</java-version>
<restlet-version>2.3.1</restlet-version>
<wtp-version>2.0</wtp-version>
</properties>
<dependencies>
<!-- Restlet core -->
<dependency>
<groupId>org.restlet.jee</groupId>
<artifactId>org.restlet</artifactId>
<version>${restlet-version}</version>
</dependency>
<!-- To embed Restlet within a servlet container -->
<dependency>
<groupId>org.restlet.jee</groupId>
<artifactId>org.restlet.ext.servlet</artifactId>
<version>${restlet-version}</version>
</dependency>
<!-- To use HTTP Client to actual make HTTP
requests under the hood -->
<dependency>
<groupId>org.restlet.jee</groupId>
<artifactId>org.restlet.ext.httpclient</artifactId>
<version>${restlet-version}</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>maven-restlet</id>
<name>Public online Restlet repository</name>
<url>http://maven.restlet.com</url>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java-version}</source>
<target>${java-version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>install</id>
<phase>install</phase>
<goals>
<goal>sources</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<configuration>
<wtpapplicationxml>true</wtpapplicationxml>
<wtpversion>${wtp-version}</wtpversion>
</configuration>
</plugin>
</plugins>
</build>
</project>
Hope it helps you,
Thierry

Related

Using github repository as maven dependency

I have one project created which contains some reusable methods which can be used in other projects by adding it as a dependency. To do the same I have added the following in pom.xml file and when I run mvn clean deploy branch is created successfully with artifacts.
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.abc</groupId>
<artifactId>api-authenticator</artifactId>
<version>1.0.0</version>
<name>API Authenticator</name>
<description>Project to authenticate API usage</description>
<distributionManagement>
<repository>
<id>internal.repo</id>
<name>Staging Repository</name>
<url>file://${project.build.directory}/${version}</url>
</repository>
</distributionManagement>
<properties>
<github.global.server>github</github.global.server>
<java.version>11</java.version>
<java-jwt.version>3.16.0</java-jwt.version>
<jwks-rsa.version>0.18.0</jwks-rsa.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>${java-jwt.version}</version>
</dependency>
<dependency>
<groupId>com.auth0</groupId>
<artifactId>jwks-rsa</artifactId>
<version>${jwks-rsa.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>com.github.github</groupId>
<artifactId>site-maven-plugin</artifactId>
<version>0.12</version>
<configuration>
<message>Maven artifacts for ${project.version}</message>
<noJekyll>true</noJekyll>
<outputDirectory>${project.build.directory}</outputDirectory>
<branch>refs/heads/${version}</branch>
<includes>
<include>**/*</include>
</includes>
<merge>true</merge>
<repositoryName>rwg-dip-api-authenticator</repositoryName>
<repositoryOwner>robert-walters</repositoryOwner>
<server>github</server>
</configuration>
<executions>
<execution>
<goals>
<goal>site</goal>
</goals>
<phase>deploy</phase>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
<configuration>
<altDeploymentRepository>
internal.repo::default::file://${project.build.directory}/${version}
</altDeploymentRepository>
</configuration>
</plugin>
</plugins>
</build>
</project>
In settings.xml (Maven home: /opt/homebrew/Cellar/maven/3.8.2/libexec) I have generated personal access token with all access required and added it.
<server>
<id>github</id>
<password>access_token</password>
</server>
To use this as a dependency I have added the following in the project where I want to reuse the functions and when I do reimport the dependency from IntelliJ I can see my internal project's dependency in external dependencies But when I run the project it shows java: package com.abc.util does not exist althoght I am getting the suggestions for the class in Intellj.
<repositories>
<repository>
<id>https://github.com/me/api-authenticator</id>
<url>https://github.com/me/api-authenticator/1.0.0</url>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>
<dependency>
<groupId>com.abc</groupId>
<artifactId>api-authenticator</artifactId>
<version>1.0.0</version>
</dependency>
I have gone through the below but it was not helpful for the above case
Hosting a Maven repository on github
https://www.baeldung.com/maven-repo-github
https://dev.to/iamthecarisma/hosting-a-maven-repository-on-github-site-maven-plugin-9ch
UPDATE
As M.Deinum has suggested I have removed spring-boot-maven-plugin and now I am able to use it as depency in other projects in Intellj and it works fine locally but when the pipeline execute to create build for the project mvn package steps fails with "The POM for com.abc:api-authenticator:jar:1.0.0 is missing, no dependency information available"
If you are ok with using an external service for that, you can check https://jitpack.io/ out.
It is able to build your maven project directly from a GitHub repository and then acts as a maven repository that is serving the artifact.
It takes time to download the artifact for the first time (as it builds the project only with the first request for the artifact), but then it reuses the already built artifacts for the next requests.

Setting up JVM debugging in Maven project

I'm trying to debug my application that's running on a remote JVM, but I can't manage to get my dependencies there too.
Here are a few things I tried:
Remote debugger:
I've set up a remote debugger, and it corrects correctly to the JVM but when I then use my application I get a bunch of NoClassDefFoundError errors coming from external libraries that are in my maven dependencies (which makes sense because I building the app with the Intellij building tools and not Maven itself. I have some relocation rules set up so maybe that's the issue?
The JVM itself is running on my local machine.
Maven debugger
There is a maven debugger integrated inside Intellij but it seems like that is just using the remote JVM during the jar packaging but nothing else. Maybe I'm going something wrong there?
I also have a few yaml files in my resources folder that should be correctly included in the debugger.
Here is my project structure:
Here is my POM file for reference:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>me.playbosswar.com</groupId>
<artifactId>commandtimer</artifactId>
<name>CommandTimer</name>
<version>6.0</version>
<description>Plugin</description>
<build>
<finalName>${project.name}_v${project.version}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<outputDirectory>/Users/tristan/Documents/minecraft/servers/1.16.5/plugins</outputDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<configuration>
<relocations>
<relocation>
<pattern>com.cryptomorin.xseries</pattern>
<shadedPattern>me.playbosswar.com</shadedPattern>
</relocation>
</relocations>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<repositories>
<repository>
<id>papermc</id>
<url>https://papermc.io/repo/repository/maven-public/</url>
</repository>
<repository>
<id>placeholderapi</id>
<url>http://repo.extendedclip.com/content/repositories/placeholderapi/</url>
</repository>
<repository>
<id>codemc-snapshots</id>
<url>https://repo.codemc.io/repository/maven-snapshots/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.destroystokyo.paper</groupId>
<artifactId>paper-api</artifactId>
<version>1.16.5-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>me.clip</groupId>
<artifactId>placeholderapi</artifactId>
<version>2.10.9</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
<dependency>
<groupId>com.github.cryptomorin</groupId>
<artifactId>XSeries</artifactId>
<version>7.2.1.2</version>
</dependency>
<dependency>
<groupId>fr.minuskube.inv</groupId>
<artifactId>smart-invs</artifactId>
<version>1.2.7</version>
</dependency>
<dependency>
<groupId>net.wesjd</groupId>
<artifactId>anvilgui</artifactId>
<version>1.5.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8.1</version>
</dependency>
</dependencies>
</project>
Here is the actual error:

Maven shade is only shading some classes from the dependency (2 of 3) into final jar

I am using Jitpack to get one of my github repositories as a dependency, and then using maven shade to include said dependency in the compiled output of the project. However, only 2 of the 3 classes in the dependency are being shaded into the final jar. There seem to be no compilation issues (syntax or maven), yet at runtime the missing class causes a NoClassDefFoundError to be thrown. Decompiling the jar shows that ConfigManager and Utils are shaded, but Logger is ignored.
Dependency: https://github.com/Benlewis9000/PluginToolsAPI
Project pom:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.benlewis9000</groupId>
<artifactId>PluginToolsAPITestPlugin</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<repositories>
<!-- This adds the Spigot Maven repository to the build -->
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
<!-- Access github repositories -->
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependencies>
<!--This adds the Spigot API artifact to the build -->
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.13.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.github.Benlewis9000</groupId>
<artifactId>PluginToolsAPI</artifactId>
<version>82aaa66bbf</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<minimizeJar>false</minimizeJar>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Please let me know if any info is lacking,
Thank you
I didn't know about Jitpack, it does look rather useful!
I've used this config in the past for shade:
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
</filter>
</filters>
</configuration>

Maven - adding a war dependency to another war

I'm developing a Jersey webapp that serves as a webhook for chatbots, currently I have a generic service that handles the basic connections and now I want to create a specific app webhook that will be able to use the generic service.
I've already added the generic webapp as a maven dependency and can use it in my workspace, but when deploying to tomcat I get error:
[org.glassfish.jersey.server.ContainerException: java.lang.NoClassDefFoundError:
.../generic/services/Webhook] with root cause
java.lang.ClassNotFoundException: ...generic.services.Webhook
Generic services pom file:
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
</parent>
<artifactId>...-generic-services</artifactId>
<name>...</name>
<groupId>...</groupId>
<packaging>jar</packaging>
<properties>
<jersey.version>2.27</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
...
</dependencies>
<build>
<finalName>...</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<inherited>true</inherited>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<attachClasses>true</attachClasses>
</configuration>
</plugin>
</plugins>
</build>
App services pom file:
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
</parent>
<artifactId>...-app-services</artifactId>
<name>...</name>
<groupId>...</groupId>
<packaging>war</packaging>
<properties>
<jersey.version>2.27</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>..generic.services</groupId>
<artifactId>...-generic-services</artifactId>
<version>...</version>
<classifier>classes</classifier>
</dependency>
</dependencies>
<build>
<finalName>...-app-services</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<inherited>true</inherited>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
So what I basically want to accomplish is to be able to call the generic services functions from the app service webapp, so for example if in generic service there is a function that receives and returns a json then I want to be able to make a function like this in app and call the generic service function with a json and receive from it.
You can't add a WAR as a Maven dependency. WAR has a specific layout which is understood by Servlet Containers and JEE Servers. This layout is not understood by other tools e.g. Maven which expects dependencies to be JARs.
This problem is normally solved by extracting either a new services-common Maven module which stores the shared code or moving the Java code outside of the WAR into a new JAR Maven module.
You can try using WAR Overlays to achieve what you want but it won't be as clean as extracting new JAR Modules.

Axis2 wsdl2java code on apache karaf

My goal is to call a third party web service. So I generated the java classes from the wsdl using axis2 wsdl2java and adb data binding.
I'm calling the web service using these generated classes which works fine.
Now I want do do this in a bundle which is deployed on the osgi container apache karaf. I am building the bundle using the maven-bundle-plugin. Here I got the problem that karaf needs all those requirements of axis2 (axiom, mime4j, commons-fileupload,etc etc).
Is there any good solution to get axis2 to work on karaf besides deploying all those dependencies as bundles on karaf (because these are quite a lot)?
This is my pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>AxWebServiceCall</groupId>
<artifactId>AxWebServiceCall</artifactId>
<packaging>bundle</packaging>
<version>1.0</version>
<name>Ax WebService calls</name>
<url></url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<version>2.15.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-blueprint</artifactId>
<version>2.15.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-cxf</artifactId>
<version>2.15.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-kernel</artifactId>
<version>1.6.3</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-adb</artifactId>
<version>1.6.2</version>
</dependency>
</dependencies>
<build>
<defaultGoal>install</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- to generate the MANIFEST-FILE of the bundle -->
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.3.7</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>AxWebServiceCall</Bundle-SymbolicName>
<Private-Package>AxWebServiceCall.*</Private-Package>
<Import-Package>*</Import-Package>
</instructions>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-wsdl2code-maven-plugin</artifactId>
<version>1.6.3</version>
<executions>
<execution>
<goals>
<goal>wsdl2code</goal>
</goals>
<configuration>
<packageName>com.foo.myservice</packageName>
<wsdlFile>src/main/resources/wsdl/axservice.wsdl</wsdlFile>
<databindingName>adb</databindingName>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
And how I call the WebService
RoutingServiceStub stub = new RoutingServiceStub(
"http://some.url");
PaygateServiceHBXProcessRequest paygateRequest = new PaygateServiceHBXProcessRequest();
PaygateContractHBX contract = new PaygateContractHBX();
contract.setLength(Integer.valueOf(data.get("length").toString()));
contract.setData(data.get("encrypted_data").toString());
paygateRequest.set_paygateContract(contract);
CallContextE context = new CallContextE();
CallContext paygateContext = new CallContext();
paygateContext.setCompany("HB2C");
context.setCallContext(paygateContext);
PaygateServiceHBXProcessResponse response = stub.process(
paygateRequest, context);
AusfuehrungErgebnis response2 = response.getResponse();
And the error I get in the karaf logs
[...]Unable to resolve 994.0: missing requirement [994.0] osgi.wiring.package; (osgi.wiring.package=org.apache.axiom.om)[...]
As I said I can solve this by deploying the axiom bundle but then it needs the next dependency which I could also deploy and so on...But as I said I want this to work without deploying numerous bundles manually.

Categories

Resources