2

I have a class (stored in server/java) that returns a Response type object and I want to use it in adapters.

public class CorsResponse {
public static Response build() {
    return Response
            .ok()
            .header("Access-Control-Allow-Origin", "*")
            .header("Access-Control-Allow-Methods", "GET, POST")
            .header("Access-Control-Allow-Headers", "accept, origin, content-type")
            .header("Access-Control-Max-Age", "1728000")
            .build();
}

If I use this class in an adapter's method, I get an exception:

javax.servlet.ServletException: java.lang.LinkageError: loader constraint violation: loader (instance of com/ibm/ws/classloading/internal/AppClassLoader) previously initiated loading for a different type with name "javax/ws/rs/core/Response" ...

Caused by: java.lang.LinkageError: loader constraint violation: loader (instance of com/ibm/ws/classloading/internal/AppClassLoader) previously initiated loading for a different type with name "javax/ws/rs/core/Response"

How can I set which Response type to load?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Eusebiu Marcu
  • 320
  • 1
  • 10

2 Answers2

1

I have managed to create an external project, put the class there, compile it to a jar file and include it in the adapters/adapter_name/lib folder (added to build path also).

Eusebiu Marcu
  • 320
  • 1
  • 10
0

If you are interested in a CorsFilter I suggest implementing it like show below and adding the class to your Application instead...

@Provider
public class ResponseCorsFilter implements ContainerResponseFilter {

    @Override
    public void filter(ContainerRequestContext requestContext,
            ContainerResponseContext responseContext) throws IOException {

        responseContext.getHeaders().add("Access-Control-Allow-Origin", "*");
        responseContext.getHeaders().add("Access-Control-Allow-Methods", "GET, POST, OPTIONS");


        String reqHead = requestContext.getHeaderString("Access-Control-Request-Headers");

        if(null != reqHead && !reqHead.equals("")){
            responseContext.getHeaders().add("Access-Control-Allow-Headers", reqHead);
        }

    }


}
hfhc2
  • 4,182
  • 2
  • 27
  • 56
  • ContainerResponseFilter is not available in MFP server libs... or it has a different name :) – Eusebiu Marcu Jul 31 '15 at 09:25
  • Well, you tagged this with jax-rs (and mentioned that in the title). The specification for jax-rs (https://jcp.org/aboutJava/communityprocess/final/jsr339/index.html) specifies the exixtence of the `ContainerResponseFilter` class. I thought you were using a jax-rs implementation?! And what do you mean by MFP? – hfhc2 Aug 02 '15 at 18:15
  • You are right on the specification. The MFP is IBM MobileFirst Platform (which includes a WebSphere Liberty Profile) which I do not think it has this class. – Eusebiu Marcu Aug 03 '15 at 08:56