I've been writing a prototype Jersey (JAX-RS) application and wanted to try handling application/x-www-form-urlencoded posts with a redirect-after-POST methodology.
I want to redirect to an html page hosted at the application root on success, however I can't seem to escape out of Jersey's servlet root.
Here's an example of a resource which allows you to create a new user:
URI I want: /jersey-test/user.html
URI I get: /jersey-test/r/user.html
#POST
#Consumes({MediaType.APPLICATION_FORM_URLENCODED})
public Response putUser(#Context UriInfo uriInfo,
MultivaluedMap<String, String> formParams) {
// snip... do work and insert user here...
URI uri = uriInfo.getBaseUriBuilder().path("user.html").build();
return Response.seeOther(uri).build();
}
Relevant snippets from my web.xml:
<web-app ...>
<display-name>jersey-test</display-name>
...
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
...
</servlet>
...
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/r/*</url-pattern>
</servlet-mapping>
</web-app>
Assign the path like this:
URI uri = uriInfo.getBaseUriBuilder().path("../user.html").build();
Related
I have a server and client pair. Server is written in java using Jersey for RESTful APIs. I am running it in Tomcat server. It is working fine for HTTP GET/POST/DELETE calls. But I want to make the calls using HTTPS. What do I need to change on server side?
<!-- language: lang-java -->
#Path("/article")
public class ArticleService {
EntityDao<Article> articleDao = new ArticleDaoImpl();
#GET
public Response greet() {
return Response.ok("Welcome to restroshop APIs...").build();
}
#GET
#Path("/read/{id}")
#Produces("application/json")
public Response readArticle(#PathParam("id") final long id) {
Article article = articleDao.read(id);
return article != null ?
Response.ok(article, MediaType.APPLICATION_JSON).build() :
Response.noContent().build();
}
#POST
#Path("/create")
public long create(Article article) {
return ((Long) articleDao.create(article));
}
#DELETE
#Path("/delete/{id}")
public Response delete(#PathParam("id") final long id) {
articleDao.delete(id);
return Response.ok("Article Deleted successfully").build();
}
}
My web.xml is as following:
<web-app>
<display-name>Restroshop Application</display-name>
<servlet>
<servlet-name>Restroshop-servlet</servlet-name>
<servlet-class>
com.sun.jersey.spi.container.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.restroshop.application</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Restroshop-servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
My Client is an android application.
Of what you've shown, none of your code needs to change. But to enable SSL depends on your setup. If you've got Apache httpd in front of Tomcat, then you'll need to start with the Apache SSL docs. If you're running only Tomcat then you'll need to take a look at the Tomcat SSL docs. Both of these processes are very well documented.
I would like to use Cutome servlet only for url which are not having extensions like .jsp,jss,css and image extensions.
I tried like this but no use.
Web.xml :
<filter>
<filter-name>ControllerFilter</filter-name>
<filter-class>tut.controller.ControllerFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ControllerFilter</filter-name>
<servlet-name>ControllerServlet</servlet-name>
</filter-mapping>
<servlet>
<servlet-name>ControllerServlet</servlet-name>
<servlet-class>tut.controller.ControllerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ControllerServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>FileServlet</servlet-name>
<servlet-class>tut.controller.FileServlet</servlet-class>
</servlet>
Filter :
String requestedUri = ((HttpServletRequest)request).getRequestURI();
System.out.println("requestedUri:"+requestedUri);
if(requestedUri.matches(".*[css|jpg|png|gif|js|jsp]*")){
//How to configure the default calling here
return;
}
else
{
// ControllerServlet for other requests
chain.doFilter(request, response);
}
Try with $ that represents The end of a line in regex pattern
requestedUri.matches(".*[css|jpg|png|gif|js|jsp]$")
If matched then follow the chain otherwise forward the request to the required Servlet, JSP or HTML.
if (uri.matches(".*[css|jpg|png|gif|js|jsp]$")) {
filterChain.doFilter(req, res);
}else{
// forward the request to Servlet/JSP/HTML
req.getRequestDispatcher("path").forward(req, resp);
}
web.xml:
use / as url pattern for filter to inspect all the request then based on uri forward it to Servlet/JSP/HTML in the filter itself.
<filter-mapping>
<filter-name>ControllerFilter</filter-name>
<servlet-name>/</servlet-name>
</filter-mapping>
Find a better solution here Servlet for serving static content
I don't think that Filter is a good mechanism for achieving what you want. If Filter gets a request for unwanted extensions, it's too late to deal with this.
You can modify your request and response in Filter to do pre-processing or post-processing, but it's probably not what you're looking for. You want those extensions not to be routed to the servlet at all, but rather be processed by a static content handler.
To do what you want you'll need to put your static content to a folder which is not under path that can match Servlet path, otherwise your servlet is going to get requests for the static content at which point you'll need to do something about those requests inside your servlet.
I have spring mvc application
if in web.xml i write so:
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
I go to http://localhost:8080/Mvc/controllerPath/sayHello
I see my page
if I write
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/controllerPath/*</url-pattern>
</servlet-mapping>
I go to http://localhost:8080/Mvc/controllerPath/sayHello - I see 404
I think you understood what I want.
Can you hel me?
UPDATE
controller:
#Controller
#RequestMapping("/controllerPath")
public class MyController {
#RequestMapping("/sayHello")
public String sayHello(Model model){
model.addAttribute("name", "Vasya");
return "hello";
}
}
if I write
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/Mvc/controllerPath*</url-pattern>
</servlet-mapping>
i see 404
If you remove:
#RequestMapping("/controllerPath")
to
#RequestMapping("/")
the new servlet mapping will work.
The reason for this is that whatever you have in the servlet mapping url is stripped before spring tries to match it to a controller.
For example, in your first url mapping with just / (and assuming your web appllication is deployed to /mvc), your path of /mvc/controllerPath/sayHello spring strips the url mapping away from the url and expects to find a controller that maps to /controllerPath/sayHello
When you change the url-pattern to /controllerPath, since will strip that as well and look for a controller that answers to just /sayHello, which your controller won't since it's expecting /controllerPath/sayHello
We have an existing Jersey REST service in our application(URL: /rest/*).
A sample URL looks like: http://xxx:8080/app/rest/company/getdata
Based on a property we need to redirect the REST call to another context(URL: /newrest/*):
A sample URL would look like: http://xxx:8080/app/newrest/company/getdata
So, I added a servlet mapping in my web.xml.
So, My web.xml looks like the below snippet.
<servlet>
<servlet-name>ServletAdaptor</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>xxx.httpservice</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ServletAdaptor</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<filter>
<filter-name>secureRESTFilter</filter-name>
<filter-class>xxx.RESTSecurityFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>secureRESTFilter</filter-name>
<url-pattern>/rest/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>NewAdaptor</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>xxx.newhttpservice</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>NewAdaptor</servlet-name>
<url-pattern>/newrest/*</url-pattern>
</servlet-mapping>
I have written a filter(to check for the property on the ServletAdaptor servlet and I need to redirect to the NewAdaptor servlet, if needed. Also, the final response should be sent back by the Servlet Adaptor servlet using the response from NewAdaptor servlet, if necessary(based on the property).
I need directions for solving this issue. Please help.
The dofilter method in my filter class looks like:
#Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws ServletException, IOException {
HttpServletRequest request = (HttpServletRequest) req;
String productMode = "b";//hardcoded for this example
String url = request.getRequestURL().toString();
if(productMode.equals("a")){
chain.doFilter(req, res);
}else{
url=url.replace("rest", "newrest");
req.getRequestDispatcher(url).forward(req, res);
}
}
But the RequestDispatcher does not seem to forward the request to the new url.
From what i understand you are trying to create new services or integrate newer one.
Ideally you can version the URL's to support backward compatibilty in future.e.g.
http://{xxx}:8080/app/rest/v1_0/company/getdata,http://{xxx}:8080/app/rest/v2_0/company/getdata etc.
Why don't you wrap the new API call in the existing API call? So that the consumer talks to your API, internally you invoke the newer API and consume the response of it.Then you either return the new object or wrap your object around it.
You need to really re-look at the strategy you are trying to adopt!
How to pass pojo object as parameter to rest web service from prototypejs client.
Assume i have web service like this.
#Path("/postItem")
#Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Item postItem(Item item)
{
return new item;
}
From client side using prototypejs library how to pass pojo object as parameter to rest web service.
If parameter is of type string or integer i would have passed it as query param but it is pojo object in case.
I am about the syntax creation of the pojo object and then passing it to rest web service from prototypejs.
Which jax-rs implementation do you use? for Jersey, you can post json to the server by specify the jersey servlet:
<servlet>
<servlet-name>jersey-servlet</servlet-name>
<servlet-class>
com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>your package</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-servlet</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
then in your client code, you can directly post a json to the server, which will be deserialized to an Item instance