JBoss7: breaking bootloader restrictions with ReastEasy and httpclient with custom HttpRequestInterceptor

I am using the RestEasy Client Framework in @Named @ViewScoped Bean with JBoss-7.1.1-Final to retrieve data from a REST service using a custom HttpRequestInterceptor :

 RegisterBuiltin.register(ResteasyProviderFactory.getInstance()); DefaultHttpClient httpClient = new DefaultHttpClient(); httpClient.addRequestInterceptor(new PreemptiveAuthInterceptor("test","test"), 0); ClientExecutor clientExecutor = new ApacheHttpClient4Executor(httpClient); //<--- //The error occurs above, the code below is only for completeness MyRest rest = ProxyFactory.create(MyRest.class, "http://localhost:8080/rest",clientExecutor); 

This works fine in a standalone client application (also good when I ClientExecutor , but I need it to authenticate the REST service). The bean is located in the WAR module inside the EAR , the resteasy dependency hierarchy allows the following:

Resteasy dependency

There is no httpclient or httpcore in the WAR or EAR . Inside the Bean, the following exception occurs:

 java.lang.NoClassDefFoundError: org/apache/http/HttpRequestInterceptor 

It seems to be easy (although I'm curious about holiday packaging), and I added org.apache.httpcomponents:httpclient with a compilation scope:

enter image description here

No, I get the following exception:

 java.lang.LinkageError: loader constraint violation: when resolving method "org.jboss.resteasy.client.core.executors.ApacheHttpClient4Executor.<init> (Lorg/apache/http/client/HttpClient;)V" the class loader (instance of org/jboss/modules/ModuleClassLoader) of the current class, my/TestBean, and the class loader (instance of org/jboss/modules/ModuleClassLoader) for resolved class, org/jboss/resteasy/client/core/executors/ApacheHttpClient4Executor, have different Class objects for the type org/apache/http/client/HttpClient used in the signature my.TestBean.init(TestBean.java:65) 

Refresh . To reproduce this, you do not need REST interfaces, an error occurs when creating an instance of ApacheHttpClient4Executor , but you may need a custom PreemptiveAuthInterceptor :

 public class PreemptiveAuthInterceptor implements HttpRequestInterceptor { private String username; private String password; public PreemptiveAuthInterceptor(String username, String password) { this.username=username; this.password=password; } @Override public void process(org.apache.http.HttpRequest request, HttpContext context) throws HttpException, IOException { AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE); authState.setAuthScope(org.apache.http.auth.AuthScope.ANY); authState.setCredentials(new UsernamePasswordCredentials(username,password)); authState.setAuthScheme(new BasicScheme()); } } 
+4
source share
4 answers

To avoid a binding error when deploying the application in JBoss, configure the org.apache.httpcomponents module in the JBoss installation modules folder, but do not include the JAR from the HttpComponents in your application:

  • Put the required JARs from the HttpComponents into modules/org/apache/httpcomponents/main .
  • List these JARs in module.xml in this directory.
  • Add Dependencies: org.apache.httpcomponents to MANIFEST.MF your component.

Please note that the module mentioned in steps 1 and 2 already exists. However, you can enable additional JARS (e.g. httpclient-cache-xyzjar ) or different versions.

Allowing classes in a development environment is, of course, another matter.

+10
source

I had the same problem in similar circumstances. The required HttpComponents module was already in my JBoss module directory (AS 7.1.1). Therefore, all I had to do to solve the problem was to add a manifest entry, as described by Eustachius, which you can do in Maven like this:

 <build> <plugins> <plugin> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifestEntries> <Dependencies>org.apache.httpcomponents</Dependencies> </manifestEntries> </archive> </configuration> </plugin> </plugins> </build> 

The configuration is the same for the maven-war plugin.

+6
source

If a module is needed in the Arquillian test ...

Create arquillian-manifest.mf in src / test / resources with the following:

 Manifest-Version: 1.0 Dependencies: org.jboss.resteasy.resteasy-jaxrs,org.apache.httpcomponents 

Then when you ShrinkWrap:

 WebArchive war = ShrinkWrap.create... .addAsManifestResource("arquillian-manifest.mf", "MANIFEST.MF") 
+2
source

But add the dependencies: org.apache.httpcomponents to MANIFEST.MF of your component.

throws an exception - Called: java.lang.IllegalStateException: JBAS014744: no META-INF / services / org.jboss.as.controller.Extension found for org.apache.httpcomponents: main on org.jboss.as.controller.parsing.ExtensionXml .loadModule (ExtensionXml.java:191) [jboss-as-controller-7.2.0.Final-redhat-8.jar: 7.2.0.Final-redhat-8]

0
source

Source: https://habr.com/ru/post/1412551/


All Articles