Adding a handler for a JAX-RS Rest service - java

For my own hosted JAX-WS (Soap) web services, I can add handlers by defining a handler-chain file and adding them there, for instance:
#WebService(serviceName = "My_Service", portName = "My_Port_Soap12", targetNamespace = "urn:mynamespace")
#HandlerChain(file = "handler-chain.xml")
public class MyService { ... }
In my handler-chain.xml:
<?xml version="1.0" encoding="UTF-8"?>
<handler-chains xmlns="http://java.sun.com/xml/ns/javaee">
<handler-chain>
<handler>
<handler-class>MyHandler</handler-class>
</handler>
</handler-chain>
</handler-chains>
Is there any way I can do the same for a JAX-RS Rest service?
I need some custom authentication logic which I need to add to my services.

Related

Why my jaxrs APIs has the application name as application path?

I created a simple version API with rest-easy and wildfly, everything should works but the application path has the name of my project:
My version API return the version number from my pom.xml: The URI should be http://localhost:8080/version
but to acces it this is http://localhost:8080/projectName/version.
To acces to the open api file : The URI should be http://localhost:8080/openapi
but this is http://localhost:8080/projectName/openapi
I tried to extend Application to set the #ApplicationPath("/") but it's not working, it just allowed me to add element on the application path
example: http://localhost:8080/projectName/test/....
How can I set the application path to the root (/) and remove the projectName ?
JaxRSActivator class:
#ApplicationPath("/")
public class JaxRSActivator extends Application {
public JaxRSActivator()
{
super();
}
}
VersionFacade class:
#Path("/version")
#Tags
public interface VersionFacade {
#GET
#Produces(TEXT_PLAIN)
#Operation(summary = "Application version",
responses = {
#ApiResponse(responseCode = "200",
description = "Version number",
content = #Content(mediaType = TEXT_PLAIN, schema = #Schema(implementation = String.class)))})
String getVersion();
}
web.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<!-- public API -->
<security-constraint>
<web-resource-collection>
<web-resource-name>public</web-resource-name>
<url-pattern>/openapi.json</url-pattern>
<url-pattern>/version</url-pattern>
</web-resource-collection>
</security-constraint>
<context-param>
<param-name>resteasy.role.based.security</param-name>
<param-value>true</param-value>
</context-param>
</web-app>
openapi-configuration.yaml file
openapi: 3.0.0
prettyPrint: true
cacheTTL: 0
openAPI:
info:
version: '0.0.1'
title: API config file
Jboss wildfly use the Web module name (projectName.war) as default context root. To set it you need to:
Add the WEB-INF/jboss-web.xml file in your Web module:
(if you don't package your app in an EAR file)
<jboss-web xmlns="http://www.jboss.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.jboss.com/xml/ns/javaee
http://www.jboss.org/j2ee/schema/jboss-web_5_1.xsd">
<!-- Set context root to / -->
<context-root>/</context-root>
</jboss-web>
Or add the META-INF/application.xml file in your EAR:
(if you package your app in an EAR file)
<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/application_8.xsd" version="8">
<display-name>projectName-ear</display-name>
<module>
<web>
<!--My Web module -->
<web-uri>projectName-ws.war</web-uri>
<!-- Set context root to / -->
<context-root>/</context-root>
</web>
</module>
<module>
<!-- other EJB module -->
<ejb>projectName-core.jar</ejb>
</module>
</application>
If you use Eclipse, careful it sometimes don't publish your application.xml so you need to put it manually in your Wildfly standalone/deployments folder.

Multiple context for java web application

I have a situation here i have an application in jsf-2.1 which is deployed as http://localhost:8080/myWebApplication and in "META-INF" i have context.xml which has the following configuration
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/myWebApplication" />
Now i want to shorten the name without loosing context => 'myWebApplication' like '/mwp'
i tried to do the following but it did not work out as expected:
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/myWebApplication" />
<Context antiJARLocking="true" path="/mwp" />
Please advise if it is possible or is there any workaround to achieve this.
Your application will (on your application server, not talking about vhosts or mod_rewrite on apache) only respond to:
The application name you have defined in your web.xml
If there is no such configuration in your web.xml, it will respond to the name of the war-file. If for example your application is called myWebApplication.war, it will respond to /myWebApplication.

how to parse xml using soap web service

I have a device which periodically sends xml file to a specific webservice. Also I have a web service. My web service read this xml.
My Web service definition is :
#Name("myWebService")
#SOAPBinding(style = Style.RPC)
#WebService(targetNamespace = "http://www.abc.com.tr/")
public class MyWebService {
#Resource
WebServiceContext wsContext;
#WebMethod()
public String DeviceStatus(#WebParam(name = "SP") SP SP) {..}
My class structure is SP.java, MODULES.java, MODULE.java, SENSORS.java, SENSOR.java
Before there was no problem but when the incoming xml's namespace definition has changed my webservice started to give error like : can not find child element SP
before the incoming xml was :
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:abc="http://www.abc.com.tr/">
<soap:Body>
<abc:DeviceStatus>
<SP>
<VERSION>1.2</VERSION>
<MODULES>
<MODULE><NAME>name</NAME><TYPE>1</TYPE>
<SENSORS>
<SENSOR><ID>0</ID><NAME>Input</NAME></SENSOR>
</SENSORS>
</MODULE>
</MODULES>
</SP>
</abc:DeviceStatus></soap:Body></soap:Envelope>
now the incoming xml is :
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<DeviceStatus xmlns="http://www.abc.com.tr/">
<SP>.......
I am looking forward your replies. thanks...

CDI injection in JAX-WS endpoint does not work, results in NPE

Why doesn't the following CDI work in JAX-WS endpoints in glassfish 3.x.x? I get an NPE when accessing the service from the endpoint.
#WebService
public class JaxWsTestEndpoint {
#Inject
private MyService service;
#WebMethod
public String sayHello(String name) {
System.out.println("injected service:" + service);
service.callService();
return "Hello, " + name + ".";
}
}
Where the class "service" is defined as follows:
#Named("myService")
public class MyService {
public MyService() {
System.out.println("init myService.");
}
public void callService() {
System.out.println("calling Service.");
}
}
I have an empty beans.xml file in WEB-INF. I tried it with complete empty content, and with an empty
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
</beans>
tag. But somehow, the service field in the JAX-WS endpoint is still NULL after deployment and during receiving a web service request, resulting in a NPE when calling the service. What am i missing here?
You can try to remove sun-jaxws.xml from WEB-INF directory. This way has helped me!
Yes I got it working by removing sun-jaxws.xml and modifying web.xml pointing my webservice directly instead of WSServlet.
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- This listener parses a sun specific configuration file (sun-jaxws.xml), which provides the web service
endpoints and connects the WSServlet instance to the services' implementation classes -->
<!--<listener>
<listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener> -->
<!-- Delegate requests whose URLs end with the path '/StakeholderWebService' to a WSServlet instance provided by container, which in turn is linked to the JWS runtime -->
<servlet>
<servlet-name>StakeholderWebService</servlet-name>
<!-- <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class> -->
<servlet-class>com.werner.stakeholder.webservices.StakeholderWebServiceImpl</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>StakeholderWebService</servlet-name>
<url-pattern>/stakeholderWebService</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>120</session-timeout>
</session-config>

Using #Resource to load environment entries

I'm trying to load bean runtime configuration.
#Stateless
public class MyBean implements MyLocal{
#Resource String runtimeSetting1="default_value";
//....
}
I cannot find out how to create custom resource on app server side (Glassfish) - I have no idea what I should enter in "Factory Class" field.
Maybe there is a better way of loading configuration...
Thanks.
To my knowledge, the standard way in Java EE is to declare env-entry for configuration data. This applies to all Java EE components like EJB 3 bean class, servlet, filters, interceptors, listener classes, etc. Here, declare something like this in your ejb-jar.xml:
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.0"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd">
<enterprise-beans>
<session>
<ejb-name>FooBean</ejb-name>
<env-entry>
<description>foobar entry</description>
<env-entry-name>foo</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>bar</env-entry-value>
</env-entry>
...
</session>
...
</enterprise-beans>
....
</ejb-jar>
Then look up the env-entry with JNDI or inject it by its name. For example, to inject it in your bean:
#Resource(name="foo")
private String myProperty;

Categories

Resources