How to use resourcebundles with jetty? - jetty

My question is how does one include ResourceBundles, in a Jetty project?
I have a webservice project named ServletEnvironment.
ServletEnvironment exports my service as an exploded war directory, containing:
[war directory]/WEB-INF/classes/{ properties files }
[war directory]/WEB-INF/web.xml
ServletEnvironment implements classes from my JettyWeb project.
JettyWeb starts a jetty server as described below:
Server server = new Server(port);
WebAppContext webapp = new WebAppContext();
webapp.setContextPath("/test");
webapp.setResourceBase("[war directory]//");
HandlerList hlist = new HandlerList();
hlist.addHandler(webapp);
server.setHandler(hlist);
server.start();
Then lastly I have a Servlet project, Servlet is referenced in my ServletEnvironments web.xml as below:
<!-- Listener -->
<listener>
<listener-class>com.basicservlet.BasicListener</listener-class>
</listener>
<!-- Servlet -->
<servlet>
<servlet-name>basicServlet</servlet-name>
<display-name>Default Basic Http Servlet</display-name>
<servlet-class>com.basicservlet.BasicHttpServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Default Servlet Mapping -->
<servlet-mapping>
<servlet-name>basicServlet</servlet-name>
<url-pattern>/servlet/</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>60</session-timeout>
<!-- 60 minutes -->
</session-config>
In my Servlet, I attempt:
ResourceBundle bundle = ResourceBundle.getBundle(bundleName, locale);
It returns null.
In my JettyWeb, right after server.start() I attempt:
URL rs = this.getClass().getResource(propertiesDir);
System.out.println("Found: " + rs.toString());
It also returns null.
Although I didn't believe that one would work.
Any help would be much appreciated please.

Properties properties;
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("application.properties");
properties = new Properties();
properties.load(inputStream);
String value = properties.getProperty(key);
This works in my case with embedded-jetty and application.properties is on classpath

Related

Deployment error with JAX-RS 2.0 RESTful webservice and tomcat 8.0

I am novice to writing REST WebServices.
Currently I am trying to write a RESTful service using jersey-2.x and tomcat 8.0
However, when I try to deploy in eclipse, it gives me error as follows :
java.lang.NoSuchMethodError: javax.ws.rs.core.Application.getProperties()Ljava/util/Map;
What I did is :
wrote below classes :
#ApplicationPath("resources")
public class RestTestApplication extends Application
{
#Override
public Set<Class<?>> getClasses() {
final Set<Class<?>> classes = new HashSet<Class<?>>();
// register root resource
classes.add(HelloResource.class);
return classes;
}
}
#Path("sayhello")
public class HelloResource
{
#GET
#Produces("text/plain")
public String sayhello ()
{
return "Hi, How are you !!";
}
}
downloaded from http://repo1.maven.org/maven2/org/glassfish/jersey/bundles/jaxrs-ri/2.12/jaxrs-ri-2.12.zip
downloaded jsr311-api-1.1.2.r612.jar
copied all *.jar files from jaxrs-ri-2.12.zip and jsr311-api-1.1.2.r612.jar to WEB-INF/lib and also imported to build path.
Edited web.xml as below :
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>RestWS</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Rest Test</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.example</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Rest Test</servlet-name>
<url-pattern>/resources/*</url-pattern>
</servlet-mapping>
</web-app>`
run as-->run on server
Getting the error mentioned above.
please let me know what I am doing wrong.
I think its because you have both jars from jaxrs-ri-2.12.zip and jsr311-api-1.1.2.r612.jar in your classpath. jsr311-api-1.1.2.r612.jar has the older implementation of JAX-RS API. Your Application class that your RestTestApplication extends from is from the jsr311-api-1.1.2.r612.jar; however at runtime the Application class from your jaxrs jar in jaxrs-ri-2.12.zip is being referred to. Removing the jsr311 jar from your WEBINF/lib should hopefully resolve the issue.
If you decompile the Application class from both the jars you will notice that the one in jsr311 jar doesn't have getProperties method and hence the java.lang.NoSuchMethodError error.

Spring MVC app with SOAP web service using WSSpringServlet

I have created a simple Spring MVC web application and trying to expose the services as SOAP based JAX-WS services using JAX-WS commons RI implementation.
After deploying my application on Tomcat 7, when I try accessing my web service, I get a message as 404 Not Found: Invalid Request. Below are my configurations, kindly help in resolving this.
web.xml
<web-app version="2.5" 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_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes SOAP Web Service requests -->
<servlet>
<servlet-name>jaxws-servlet</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSSpringServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jaxws-servlet</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
WEB-INF/spring/appServlet/servlet-context.xml
<beans:bean id="customerService" class="com.home.service.CustomerService" />
<!-- Web Service definition -->
<beans:bean id="customerWS" class="com.home.ws.CustomerWS">
<beans:property name="customerService" ref="customerService" />
</beans:bean>
<wss:binding url="/ws/CustomerServ">
<wss:service>
<ws:service bean="#customerWS" />
</wss:service>
</wss:binding>
CustomerWS.java
#WebService
#SOAPBinding(style = Style.DOCUMENT, use = Use.LITERAL)
public class CustomerWS {
private CustomerService customerService;
#WebMethod
public Customer read(long id) {
return customerService.read(id);
}
public void setCustomerService(CustomerService customerService) {
this.customerService = customerService;
}
}
CustomerService.java
#Service
public class CustomerService {
public Customer read(long id) {
Customer cust = null;
System.out.println("CustomerService.read invoked");
return cust;
}
}
pom.xml - included the dependency of jaxws-spring
<dependency>
<groupId>org.jvnet.jax-ws-commons.spring</groupId>
<artifactId>jaxws-spring</artifactId>
<version>1.9</version>
</dependency>
There are no errors while building or deploying the application. When I access the URL, still I see no errors in the server log files. However, the browser displays the message - 404 Not Found: Invalid Request
URL I am trying is - http://localhost:8080/crrs/ws/CustomerServ?wsdl
If I access my HomeController, it works fine. Home page is loaded as expected.
Appreciate any help. Thanks in advance.
I'm trying to do the same thing. My code is almost the same, I'm just using #Name and #Inject instead of #Service.
Just added extends SpringBeanAutowiringSupport to the #WebService class and it's working
The servlet mappings in the web.xml seem to be the cause.
<servlet-mapping>
<servlet-name>jaxws-servlet</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
[...]
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
The spring DispatcherServlet named appServlet takes care of all urls after http://localhost:8080/crrs, even http://localhost:8080/crrs/ws/CustomerServ?wsdl.
The WSSpringServlet url-pattern cannot be reached.

Spring Integration inbound webservices wsdl generation

I am trying to evaluate Spring Integration specially interested in exposing a simple POJO based service into a SOAP based webservice via service activator. Currently I am stuck & have issue generating dynamic wsdl. The WSDL is not loaded & browser shows 404 error.
I try to access with the following url on my local
http://localhost:8080/ws-inbound-gateway/echoService
http://localhost:8080/ws-inbound-gateway/echoService/echoService.wsdl
Below is the configuration
inbound-gateway-config.xml
<int:channel id="inbound" />
<bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="contextPath" value="com.manish.schema.generated" />
</bean>
<int-ws:inbound-gateway id="empServiceGateway"
request-channel="inbound" marshaller="marshaller"
unmarshaller="marshaller" />
<int:service-activator input-channel="inbound"
requires-reply="true" ref="employeeServiceActivator" method="getEmployeeDetails">
</int:service-activator>
<bean id="employeeServiceActivator"
class="org.springframework.integration.samples.ws.EmployeeServiceResponder" />
<bean id="employeeService" class="com.manish.service.EmployeeService" />
EmployeeService is just a pojo class while EmployeeServiceResponder is a service activator that invokes method on a service class.
For dynamic wsdl generation
spring-ws-config.xml
<import resource="classpath:/META-INF/spring/integration/inbound-gateway-config.xml" />
<sws:dynamic-wsdl id="echoService" portTypeName="empServiceGateway" locationUri="/echoService" targetNamespace="http://manish.niyati.com/echo">
<sws:xsd location="/WEB-INF/echo.xsd"/>
</sws:dynamic-wsdl>
<bean
class="org.springframework.ws.server.endpoint.mapping.UriEndpointMapping">
<property name="defaultEndpoint" ref="empServiceGateway"></property>
</bean>
web.xml
<servlet>
<servlet-name>spring-ws</servlet-name>
<servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/spring-ws-config.xml</param-value>
</init-param>
<init-param>
<param-name>transformWsdlLocations</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-ws</servlet-name>
<url-pattern>/echoService</url-pattern>
</servlet-mapping>
Kindly let me know what else is missing in order to make this service accessible as web-service via SI.
Also when I try to access the service from using WebService template I get the SOAPFAULT
02:18:59.436 INFO [main][org.springframework.ws.soap.saaj.SaajSoapMessageFactory] Creating SAAJ 1.3 MessageFactory with SOAP 1.1 Protocol
02:18:59.437 DEBUG [main][org.springframework.ws.soap.saaj.SaajSoapMessageFactory] Using MessageFactory class [com.sun.xml.internal.messaging.saaj.soap.ver1_1.SOAPMessageFactory1_1Impl]
02:18:59.484 DEBUG [main][org.springframework.ws.client.core.WebServiceTemplate] Opening [org.springframework.ws.transport.http.HttpUrlConnection#249fa95c] to [http://localhost:8080/ws-inbound-gateway/echoService]
02:18:59.519 TRACE [main][org.springframework.ws.soap.saaj.support.SaajUtils] SOAPElement [com.sun.xml.internal.messaging.saaj.soap.ver1_1.Envelope1_1Impl] implements SAAJ 1.3
02:18:59.535 TRACE [main][org.springframework.ws.soap.saaj.support.SaajUtils] SOAPElement [com.sun.xml.internal.messaging.saaj.soap.ver1_1.Body1_1Impl] implements SAAJ 1.3
02:18:59.562 TRACE [main][org.springframework.ws.client.MessageTracing.sent] Sent request [<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><ed:employeeRequest xmlns:ed="http://manish.niyati.com/echo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <ed:empid>100</ed:empid> </ed:employeeRequest></SOAP-ENV:Body></SOAP-ENV:Envelope>]
02:18:59.604 TRACE [main][org.springframework.ws.client.MessageTracing.received] Received response [<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><SOAP-ENV:Fault><faultcode>SOAP-ENV:Server</faultcode><faultstring xml:lang="en">**java.lang.NullPointerException**</faultstring></SOAP-ENV:Fault></SOAP-ENV:Body></SOAP-ENV:Envelope>] for request [<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><ed:employeeRequest xmlns:ed="http://manish.niyati.com/echo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <ed:empid>100</ed:empid> </ed:employeeRequest></SOAP-ENV:Body></SOAP-ENV:Envelope>]
02:18:59.605 DEBUG [main][org.springframework.ws.client.core.WebServiceTemplate] Received Fault message for request [SaajSoapMessage {http://manish.niyati.com/echo}employeeRequest]
02:18:59.607 TRACE [main][org.springframework.ws.soap.saaj.support.SaajUtils] SOAPElement [com.sun.xml.internal.messaging.saaj.soap.ver1_1.Fault1_1Impl] implements SAAJ 1.3
Thanks in Advance
-MS
To get the WSDL, change the web.xml url-pattern to <url-pattern>/*</url-pattern>.
and the URL is http://localhost:8080/ws-inbound-gateway/echoService.wsdl.
Everything else looks good.
Regarding the WebServiceTemplateQuestion, what are you sending? It looks like you are using the ws sample app, which uses a WebServiceTemplate...
#Test
public void testWebServiceRequestAndResponse() {
StringResult result = new StringResult();
Source payload = new StringSource(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<echoRequest xmlns=\"http://www.springframework.org/spring-ws/samples/echo\">hello</echoRequest>");
template.sendSourceAndReceiveToResult(WS_URI, payload, result);
logger.info("RESULT: " + result.toString());
assertThat(result.toString(), equalTo(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<echoResponse xmlns=\"http://www.springframework.org/spring-ws/samples/echo\">hello</echoResponse>"));
}
And that works just fine.
Looks like your NPE is on the server - take a look at the server logs to see what happened.
You are doing it in a much more complex way. With a simple configuration and using some basic annotations, you can develop a web service in a short time.
I have myself used Apache CXF for web service development and it is quite good for spring based configuration. You can also take a look at this blog. It has shown all the steps with figures to develop a web service with the generation of wsdl and a client to consume the web service.

How to the Change URL Patern so that it can access normal JSP also

This is my web.xml for my webservices developed using Apache CXF project
As a result of this url pattern , even if i want to access a small HTMl page using
http:localhost:8080/ZTWeb/Test.html
This si result in No Service Found ( As i guess each and every request is being treated as Web service itself )
Please tell me how to resolve this , Means how can i access the HTML pages . JSP pages inside the web application
<web-app>
<servlet>
<display-name>CXF Servlet</display-name>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>*</url-pattern>
</servlet-mapping>
</web-app>
Map the CXF Servlet to another location, for example /services/*

Is it a REST web service?

I am working on a legacy application which have a .war with following structure (and I want to add my servlet in this war)
myApp.war
- axis2-web (Downloaded from http://ws.apache.org/axis2/download/1_1/download.cgi)
- META-INF
-- MANIFEST.MF
- WEB-INF
-- classes (But it don't have any `.class` file , it have `log4j.properties` file)
-- conf (Contains `axis2.xml`)
-- lib (contains many jars)
-- modules
-- services (Some `.aar` files)
-- web.xml
Here is the web.xml file
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>Apache-Axis2</display-name>
<servlet>
<servlet-name>AxisServlet</servlet-name>
<display-name>Apache-Axis Servlet</display-name>
<servlet-class>
org.apache.axis2.transport.http.AxisServlet</servlet-class>
<!--<init-param>-->
<!--<param-name>axis2.xml.path</param-name>-->
<!--<param-value>/WEB-INF/conf/axis2.xml</param-value>-->
<!--<param-name>axis2.xml.url</param-name>-->
<!--<param-value>http://localhot/myrepo/axis2.xml</param-value>-->
<!--<param-name>axis2.repository.path</param-name>-->
<!--<param-value>/WEB-INF</param-value>-->
<!--<param-name>axis2.repository.url</param-name>-->
<!--<param-value>http://localhot/myrepo</param-value>-->
<!--</init-param>-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>AxisRESTServlet</servlet-name>
<display-name>Apache-Axis Servlet (REST)</display-name>
<servlet-class>
org.apache.axis2.transport.http.AxisRESTServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>AxisAdminServlet</servlet-name>
<display-name>Apache-Axis AxisAdmin Servlet (REST)</display-name>
<servlet-class>
org.apache.axis2.transport.http.AxisAdminServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AxisRESTServlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/servlet/AxisServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>*.jws</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AxisAdminServlet</servlet-name>
<url-pattern>/axis2-admin/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>/axis2-web/index.jsp</welcome-file>
</welcome-file-list>
<error-page>
<error-code>404</error-code>
<location>/axis2-web/Error/error404.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/axis2-web/Error/error500.jsp</location>
</error-page>
</web-app>
Is it a REST web service ? I Googled and found some tutorials on REST which says REST services have services.xml file but I couldn't find it in my application. I want to integrate my servlet in above war so what will be the right way to do it ?
PS: Please let me know if I am missing anything.
As I understand RESTFUL WS approach that the it has a traditional http request and response with get ,post ,put or delete operations directly passed to the web service and so the response doesn't have any special format (JSON-RPC,SOAP) .
For example , if we write simple code some-service.jsp and put this code on our server
the restful tells us to call the service using any http client passing it the required params
and then we will get the service response .
Now if your application meets this , it would be RESTful .
Note : there are no any specific implementation techniques for REST it is just a WS approach
hope this would help you