Output HTTP responses with Jersey Test Framework? - unit-testing

Can the Jersey Test Framework be configured to output the raw HTTP responses when a test case fails? I'd like to be able to see the detailed error information returned by the server.

You can enable HTTP logging like so
public class SimpleTest extends JerseyTest {
// ...
#Override
protected Application configure() {
enable(TestProperties.LOG_TRAFFIC);
enable(TestProperties.DUMP_ENTITY);
// ...
}
}
More details here.
This is for the latest version of the framework, currently 2.3.1.

You can always enable the normal Jersey logging for Request and Responses and see what is shows in the console:
<init-param>
<param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
<param-value>com.sun.jersey.api.container.filter.LoggingFilter</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
<param-value>com.sun.jersey.api.container.filter.LoggingFilter<</param-value>
</init-param>
Enable logging programmatically:
servletHolder.setInitParameter("com.sun.jersey.spi.container.ContainerRequestFilters" , "com.sun.jersey.api.container.filter.LoggingFilter");
servletHolder.setInitParameter("com.sun.jersey.spi.container.ContainerResponseFilters" , "com.sun.jersey.api.container.filter.LoggingFilter");

Related

unable to add additional APIs endpoint classes to GCP endpoints v2

I have started an Google cloud endpoints v2 project using the starter code provided by Google which exposes one API named "echo".
My aim is to add another exposed class (one class/api name per business module).
Once deployed, all calls to the new API are not generating a "NOT FOUND" error.
As a solution i have tried to look into the web.xml and did the below changes.
Initial WEB.XML version:
<!-- Route API method requests to the backend. -->
<servlet-mapping>
<servlet-name>EndpointsServlet</servlet-name>
<url-pattern>/_ah/api/*</url-pattern>
</servlet-mapping>
I have added my new servlet and tried to add a new servlet mapping as below:
<servlet-mapping>
<servlet-name>EndpointsServlet</servlet-name>
<url-pattern>/_ah/api/echo/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>UserEndpointsServlet</servlet-name>
<url-pattern>/_ah/api/user/*</url-pattern>
</servlet-mapping>
Now after adding the /echo/* to the initial servlet mapping, the ECHO working services stopped servicing, and the system still cannot invoke the new service.
Does anyone knows what i am doing wrong and what is the solution?
I don't think this feature is not supported by the endpoints v2 framework since it does seem a good design practice to do this separation.
You should use the same servlet, using an init-param with all of the Endpoint classes you need:
<init-param>
<param-name>services</param-name>
<param-value>com.example.echo.Echo,com.example.echo.Echo2</param-value>
</init-param>
You should not use /_ah/api/echo/* or /_ah/api/user/* in your web.xml. Instead, use #Api(name = "echo") or #Api(name = "user") and bind EndpointsServlet to /_ah/api/*.

PrimeFaces push with AWS

I am using PrimeFaces 5.0 and tomcat 7 and my application is in AWS.
I am trying to use PrimeFaces push. I have implemented a sample application and it is working with my local system. If i deploy the code to AWS it fails.
Xhtml code
<p:socket onMessage="jsFunctionToCall" channel="/service/push"/>
Web.xml
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>org.atmosphere.cpr.AtmosphereInterceptor</param-name>
<param-value>org.atmosphere.interceptor.ShiroInterceptor</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>Push Servlet</servlet-name>
<servlet-class>org.primefaces.push.PushServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>Push Servlet</servlet-name>
<url-pattern>/primepush/*</url-pattern>
</servlet-mapping>
Java code
#PushEndpoint("/service/push")
public class ServicePushResource {
#OnMessage(encoders = {JSONEncoder.class})
public String onMessage(String count) {
return count;
}
}
Pushing message:
EventBus eventBus = EventBusFactory.getDefault().eventBus();
eventBus.publish("/service/push", String.valueOf("msg"));
I am getting the error message that
WebSocket connection to 'ws://example.com/primepush/service/push?X-Atmosphere-tracking-id=0&X-Atmosphere-Framework=2.2.4-javascript&X-Atmosphere-Transport=websocket&X-Atmosphere-TrackMessageSize=true&X-atmo-protocol=true' failed: Error during WebSocket handshake: Unexpected response code: 501
After this error there is another error message that
Websocket failed. Downgrading to Comet and resending push.js.xhtml?ln=primefaces&v=5.1:1
Failed to load resource: the server responded with a status of 501 (Not Implemented) http://example.com/primepush/service/push?X-Atmos…ng&X-Atmosphere-TrackMessageSize=true&X-atmo-protocol=true&_=1422008343786
On inspecting both the error message, I found that the p:socket component is failing with transport as 'WebSocket' and it is trying with 'long-polling' which is not implemented. So my question is whether AWS support 'WebSocket' if not what is the workaround for that?
Currently WebSocket is not supported with AWS. The work aroud is to use AWS EB loadbalancer with TCP/SSL. I did this by changing the protocol from HTTP to TCP in loadbalancer configuration. But this won't work alone, need to tell beanstalk to communicate directly to tomcat in case of websocket. This can be done using a configuration file which can be added as specified in this post.
Documentation of Primefaces 5.0 says to use Atmosphere 2.1.3. I had had similar problem as you when I tried to use 2.2.x version. The current version I use is 2.1.4 and it works for me fine. Maybe you could try this also.
The other case is that firefox browser throws wss connection error (but primepush works anyway).
Please refer also to:
https://bugzilla.mozilla.org/show_bug.cgi?id=594502

Using Jersey 2.5 on Websphere 8.X

I am using the following configurations for my Rest-WS application and it deploys/works well on WAS 7.x.
But when i deploy the same on WAS8.0, i get the following exception:
(java.lang.NoSuchMethodError:
javax/ws/rs/core/Application.getProperties()Ljava/util/Map)
I understand that WAS 8.X has a JAX-RS-1.0 runtime and since the 1.0 api interface does not have the Application::getProperties(), i get the exception above.
So how do i get my project to work on WAS 8.X. Any pointers would be much appreciated..
My current application configuration as below -
JAX-RS-2.0/JSR339
JERSEY 2.5
JACKSON 2.3
WEB.XML
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.packagename.resources.MyAppResourceConfig</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
MyAppResourceConfig.java
public class MyAppResourceConfig extends ResourceConfig {
public MonitoringResourceConfig() {
// Registers JAX-RS Components
register(RequestContextFilter.class);
register(MyRestResource.class);
packages( "com.packagename");
}
}
MyRestResource.java contains #path definitions.
P.S - I tried setting the JVM property DisableIBMJAXRSEngine in WAS, but it doesn't help.
I'm having the same trouble with Jersey 2.6 and WAS 8.0.0.3. I tried changing the class loading policy to parent last (on both, web module and application) and putting the Jersey jars into the application. It solved that problem, but is too restricted for my context.

Spring web-scoped beans and axis2

I want to use spring web-scoped beans in web-service written on AXIS2 framework.
How to configure it?
Axis2 and Spring documentation disagree with each other.
Axis2 documentation says:
For the purpose of this example, we'll configure Spring via a WAR file's web.xml. Let's add a context-param and a listener:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
Spring documentation says:
When using a Servlet 2.4+ web container, with requests processed outside of Spring's DispatcherServlet (e.g. when using JSF or Struts), you need to add the following javax.servlet.ServletRequestListener to the declarations in your web application's 'web.xml' file.
<web-app>
...
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
...
</web-app>
When I use Axis2-recommended ContextLoaderListener with Spring web-scoped beans I got on deploy
java.lang.IllegalStateException: No thread-bound request found: Are
you referring to request attributes outside of an actual web request,
or processing a request outside of the originally receiving thread? If
you are actually operating within a web request and still receive this
message, your code is probably running outside of
DispatcherServlet/DispatcherPortlet: In this case, use
RequestContextListener or RequestContextFilter to expose the current
request.
When I use Spring-recommeded RequestContextListener I got running Web-service with fault on requests:
<faultstring>The SERVICE_OBJECT_SUPPLIER parameter is not specified.</faultstring>
In other words: how to configure AXIS2 with Spring and RequestContextListener?
No such functionality in AXIS2. See request https://issues.apache.org/jira/browse/AXIS2-5467 "Extend Spring support to accept web-scoped beans" for detail.

Regarding Spring and Apache CXF Integration

Inside the applicationcontext.xml file we have like this
<bean id="vincent" class="com.bayer.vincent.service.vincent"/>
<jaxws:endpoint
id="vincentSOAP"
implementor="#vincent"
implementorClass="com.bayer.vincent.service.vincent"
address="/vincent/soap"
bindingUri="http://schemas.xmlsoap.org/wsdl/soap/" />
what does this mean by this defination ??
My question is how the vincent class is being getting called ??
CXF has provided a custom spring namespace to help easily configure a webservice endpoint here.
If the implementor starts with a #, CXF makes the assumption that the endpoint is a Spring Bean, the way it is in your case.
The endpoint will have to be a normal JAX-WS endpoint, i.e annotated with #Webservice annotation, eg:
#WebService(serviceName="MemberService", endpointInterface="org.bk.memberservice.endpoint.MemberEndpoint", targetNamespace="http://bk.org/memberservice/")
Now any call to your uri-/vincent/soap, will be redirected by the CXF front controller(which you can register in the web.xml file):
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
which maintains an internal registry of payload uri's to handlers(in this case the Spring bean) and dispatches the request appropriately.
As far I understand there created proxy class which forwards all calls to your real class.
See also Configuring an Endpoint where described all jaxws:endpoint attributes.