I am facing an 'HTTP 403 Forbidden' error while trying to consume PUT request of a restful resource from an angular client. I created this restful resource using jersey and I am using tomcat 7 as application server.
Here is my resource code:
#Path("/doc")
public class DocResource {
#PUT
#Consumes(MediaType.MULTIPART_FORM_DATA)
#Produces(MediaType.APPLICATION_JSON)
#Path("file/upload")
public Response uploadFile(MultipartBody body, #QueryParam("ID") long ID) {
try {
Attachment attachment = body.getAttachment("file");
MultivaluedMap<String, String> headers = attachment.getHeaders();
String fileName = getFileName(headers);
DataHandler dataHandler = attachment.getDataHandler();
InputStream inputStream = dataHandler.getInputStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] tmp = new byte[4096];
int ret = 0;
while ((ret = inputStream.read(tmp)) > 0) {
bos.write(tmp, 0, ret);
}
// TODO - Save contents as process attachment
byte[] contents = bos.toByteArray();
return Response.ok(getDocumentService().createAttachment(ID, fileName, contents, attachment.getContentType()), MediaType.APPLICATION_JSON).build();
} catch (Exception e) {
return handleException(e, "failed to upload Attachement");
}
}
}
Here is my angular js snippet
this.uploadFile = function uploadFile(callback, ID, file) {
var baseRestURL="http://localhost:8080/rest/doc"
// resource query
var query ;
// create form data
var formData = new FormData();
formData.append('file', file);
// set up the resource
var resource = $resource(baseRestURL + '/file/upload', {
ID: ID
}, {
'ID': ID,
'upload': {
method: 'PUT',
headers: {
'Content-Type': 'multipart/form-data'
}
}
});
resource.upload(query, formData).$promise.then(function success(response) {
if (callback) callback(response);
}, function error() {
//TODO handle error
});
};
I want to notice that other type of http calls such as DELETE, POST and GET are working properly. I have only problems with PUT calls.
I had a similar issue, but both DELETE and PUT were not working for me - returned HTTP 403 error. After I searched I stumbled across this link, and it pointed out where I went wrong.
I had a CORS filter added in my web.xml as part of some other R&D i was doing, and forgot to remove it.
This was from the original web.xml
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>
io.swagger.jaxrs.listing,
com.xxx.yyy.rest</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>
org.glassfish.jersey.jackson.JacksonFeature;
org.apache.catalina.filters.CorsFilter
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
Once I removed the org.apache.catalina.filters.CorsFilter it started to work for me!
Hope this helps!
Related
I am trying to send a JSON string from a HTML page using AJAX call to a RESTWebservice. The methods in the server gets invoked however I am not able to retrieve the JSON data that I have set in the browser through AJAX call. I am usng jersy for the REST services.
Here is my HTML code.
<body>
<script type="text/javascript">
var userConfig = {};
userConfig.user = "arin_12";
userConfig.fullName = "Arindam";
var data = JSON.stringify(userConfig);
alert(data);
var req = new XMLHttpRequest();
req.open('POST', 'http://localhost:8080/LiveHive2/rest/hello', true);
req.setRequestHeader('Content-Type','application/json;charset=UTF-8');
req.onreadystatechange = function () {
if(req.readyState === 4 && req.status === 200) {
if(req.responseText) {
alert('The saving of data is ' + req.responseText);
}
}
}
req.send(data);
</script>
index page
</body>
Here is my JavaCode in RestWebservice.
#POST
#Produces(MediaType.APPLICATION_JSON)
#Consumes(MediaType.APPLICATION_JSON)
public String sayJSONHello2(UserConfig uc) {
System.out.println("req" + uc);
return "{\"Name\":\"Arindam\"}";
}
Web.xml looks like this.
<servlet>
<servlet-name>JerseyRESTService</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.vogella.jersey.first</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>
So the body of the POST request will be
{
"user": "arin_12",
"fullName": "Arindam"
}
Write a Java class this JSON can be mappend to.
public class UserConfig {
private String user;
private String fullName;
// Constructor, Getter, Setter, ...
}
Then JAX-RS allows you to automatically convert the JSON to a class instance.
#POST
#Produces(MediaType.APPLICATION_JSON)
#Consumes(MediaType.APPLICATION_JSON)
public String sayJSONHello2(UserConfig userConfig) {
System.out.println("got UserConfig: " + userConfig);
return "{\"Name\":\"Arindam\"}";
}
It is also possible to let JAX-RS handle the mapping of a result to JSON. Let's write a second class for the response.
public class HelloResponse {
private String name;
// Constructor, Getter, Setter, ...
}
Change your JAX-RS method to return an instance of this class.
#POST
#Produces(MediaType.APPLICATION_JSON)
#Consumes(MediaType.APPLICATION_JSON)
public HelloResponse sayJSONHello2(UserConfig userConfig) {
System.out.println("got UserConfig: " + userConfig);
return new HelloResponse(userConfig.getName());
}
This question already has answers here:
HTTP Status 404 - Servlet [ServletName] is not available
(4 answers)
Closed last year.
I'm trying to get a web page to send JSON data to a java servlet via a jQuery ajax POST.
I've already checked everything I could think of, but I still can't figure out why I keep getting a 404.
Even more confusing is that other calls to the same context path work correctly.
My web.xml
<web-app>
<servlet>
<servlet-name>Controller</servlet-name>
<servlet-class>com.vibridi.klyr.servlet.Controller</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>CustomerServlet</servlet-name>
<servlet-class>com.vibridi.klyr.servlet.CustomerServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Controller</servlet-name>
<url-pattern>/klyr</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Controller</servlet-name>
<url-pattern>/home</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>CustomerServlet</servlet-name>
<url-pattern>/klyr/customer/*</url-pattern>
</servlet-mapping>
My ajax call:
$.ajax({
url: "customer/save",
type: "POST",
data: JSON.stringify(o),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(obj) {
alert('Customer saved');
},
error: function(obj) {
alert('Error!');
}
});
My servlet:
public class CustomerServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static Logger logger = Logger.getLogger("KLYR_LOGGER");
private CustomerManager manager;
public void init(ServletConfig sconfig) throws ServletException {
super.init(sconfig);
manager = new CustomerManager();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//stuff
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
response.setContentType("application/json;charset=utf-8");
try {
StringBuffer sb = new StringBuffer();
String line = null;
BufferedReader reader = request.getReader();
while((line = reader.readLine()) != null) {
sb.append(line);
}
manager.saveCustomer(sb.toString());
} catch(Exception e) {
logger.log(Level.SEVERE, "Data processing failure: " + e.getMessage());
out.write(Convertor.createBaseJSON(JSONType.E).toString());
out.close();
}
out.write(Convertor.createBaseJSON(JSONType.S).toString());
out.close();
}}
}
I can see from the Chrome's debugger tools that the call is properly directed to http://localhost:8080/klyr/customer/save but it 404's, whereas http://localhost:8080/klyr does not.
Thanks a lot!
EDIT:
I've tried to switch the servlet mappings over, i.e. /klyr (the working one) on CustomerServlet and /customer/save on Controller, but nothing happens, in fact when I call /klyr from the browser bar instead of seeing the response from CustomerServlet.doGet I still see the welcome page as if Controller.doGet fired. It looks like tomcat isn't reloading the web.xml file even if I restart it. Any ideas?
This is obvious because your CustomerServlet does not bind to $.ajax({url: "customer/save", ... so it won't work, your should change the below code :
<servlet-mapping>
<servlet-name>CustomerServlet</servlet-name>
<url-pattern>/klyr/customer/*</url-pattern>
</servlet-mapping>
to something like:
<servlet-mapping>
<servlet-name>CustomerServlet</servlet-name>
<url-pattern>/customer/save</url-pattern>
</servlet-mapping>
in order to solve the problem ~
I've eventually found the culprit, gonna post it here as a reference for other people.
Both servlets mapped in my web.xml are loaded on startup.
The first servlet attempted to read a config file from an incorrect path inside its init() method, but couldn't find it and threw an exception. The Catalina startup routine exited before it could load the second servlet, hence the 404 error.
I'm working on a restlet built on restlet 1.1.1
The issue I'm facing is setting the 'Access-Control-Allow-Origin' header to allow for cross domain requests. I've attempted a few things that didn't work.
Method one, put the header in the acceptRepresentation function:
#Override
public void acceptRepresentation( Representation resetEntity ) {
Form headers = (Form)getResponse().getAttributes().get("org.restlet.http.headers");
if (headers == null) {
headers = new Form();
getResponse().getAttributes().put("org.restlet.http.headers", headers);
}
headers.add("Access-Control-Allow-Origin","https://origin.server.edu");
//other code here for actual resource logic...
}
This didn't work. I still received errors when attempting to send a request using JQuery as such:
jQuery.ajax({
type: "POST",
contentType: "application/json",
url: "https://test.servername.edu/cas/cas-rest-api/reset/",
data: JSON.stringify("{\"uname\" : \"someone\", \"attr\":\"dataElement\" }"),
dataType: "json",
crossDomain: true
})
.done(function(data){
console.log("Success");
alert(data);
})
.fail(function(data){
console.log("failure");
console.log(data);
alert(data);
});
This didn't work. So I noticed an init function in the resource class. I figured I'd attempt putting my code there to see if this would change the situation.
#Override
public void init(Context context, Request request, Response response ){
Form headers = (Form)response.getAttributes().get("org.restlet.http.headers");
if (headers == null) {
headers = new Form();
response.getAttributes().put("org.restlet.http.headers", headers);
}
headers.add("Access-Control-Allow-Origin","https://origin.server.edu");
super.init(context, request, response);
}
Nope. Didn't work either. What am I missing here? Where do I set this header?
Thanks for your replies. After some analysis of the problem it turned out that I needed to configure Spring to allow option requests for my restlet in the web.xml file as shown below:
<servlet>
<servlet-name>ccrest</servlet-name>
<servlet-class>com.noelios.restlet.ext.spring.RestletFrameworkServlet</servlet-class>
<init-param>
<param-name>dispatchOptionsRequest</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
I´m trying to use DeferredResult with an ajax request, but when I return the result my ajax request keeps waiting for response.
Here the client that make the long polling request
(function poll() {
var SUCCESS = 1;
var tenantName = $("#tenantName").val();
$.ajax({
method: "GET",
url: "url.do?subStep=" + subStep + "&tenantId=" + tenantName,
success: function (response) {
alert(response)
if (response.status === SUCCESS) {
bootbox.dialog({
size:"small",
message: "<div class='col-xs-offset-4'>" +
"<i class='fa fa-user'> Page has been modify by " + response.payload + " " +
"<a href='javascript:void(0)' onclick='reload()'>reload</a>" +
"</i>" +
"</div>"
});
}
}, dataType: "json", complete: poll, timeout: 60000
});
})();
Here my controller where I make a long polling request by Ajax every 30 seconds.
#RequestMapping(method = RequestMethod.GET)
#ResponseBody
public DeferredResult<JsonEntity> getSubStepStatus(#RequestParam("subStep") String subStep,
#RequestParam("tenantId") String tenantId) {
if (tenantsInStep.get(subStep) == null) {
initializeTenantsInStep(subStep);
}
if(!tenantsInStep.get(subStep).contains(tenantId)){
tenantsInStep.get(subStep).add(tenantId);
}
DeferredResult<JsonEntity> deferredResult = new DeferredResult<>();
tenantsDeferredInStep.put(tenantId, deferredResult);
return deferredResult;
}
And then when my application detect a POST/PUT ajax request invoke a request to this method of my controller that release the DeferredResults.
#RequestMapping(value = "/setModification", method = RequestMethod.GET)
#ResponseBody
public final Integer setModification(#RequestParam("subStep") String subStep,
#RequestParam("tenantId") String tenantId) {
for (String tenantInStepId : tenantsInStep.get(subStep)) {
if (!tenantId.equals(tenantInStepId)) {
JsonEntity<String> jsonEntity = new JsonEntity();
jsonEntity.setPayload(tenantId);
jsonEntity.setStatus(FeedbackActions.SUCCESS.getCode());
DeferredResult<JsonEntity> jsonEntityDeferredResult = tenantsDeferredInStep.get(tenantId);
tenantsInStep.remove(tenantInStepId);
jsonEntityDeferredResult.setResult(jsonEntity);
}
}
return FeedbackActions.SUCCESS.getCode();
}
This example has been done by two user with two browser to achieve this, but the first client that create the deferredResult dont receive the result when the second client invoke the deferredResult.setResult(bla), no alert on ajax code is invoked.
I´m doing something wrong?, I add the
<task:annotation-driven/>
And the
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/webmvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
In my applicationContext and web.xml configuration
As an extra information I´m using on my local test Jetty
Regards.
Just started to write my JSON webservices for a carpool engine. I am getting a HTTP 404 error as I try to write my registration API's.
This is where my problem is
"http://localhost:8081/mCruiseOnCarPool4All/carpool4all/Registration"
HTTP/1.1 405 Method Not Allowed
"http://localhost:8081/mCruiseOnCarPool4All/carpool4all/Registration/Request"
HTTP/1.1 500 Internal Server Error
"http://localhost:8081/mCruiseOnCarPool4All/Registration/Request"
HTTP/1.1 404 Not Found
"http://localhost:8081/mCruiseOnCarPool4All/Registration"
HTTP/1.1 404 Not Found
I know I am missing something really silly here.
Web.xml (Jersey Library)
<servlet>
<servlet-name>Jersey REST Service</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.mcruiseon.carpool4all</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/carpool4all/*</url-pattern>
RegistrationService.java, only coded the Post method. I am returning errors on all exceptions and ok with sessionkey when successful. You can ignore the code in the post method, I just wanted to share so that you understand my error handling.
package com.mcruiseon.carpool4all;
#Path("/Registration")
public class RegistrationService {
private ClientSession clientSession ;
private String sessionKey ;
private SessionManager sessionManager ;
#Context
UriInfo uriInfo;
#Context
Request request;
#POST
#Path ("Request")
#Consumes({ MediaType.APPLICATION_JSON })
public Response post(JAXBElement<AMessageStrategy> element) {
try {
clientSession = new ClientSession(God.mCruiseOnServer) ;
} catch (InvalidServerDNSorIPException e) {
e.printStackTrace();
return Response.serverError().build() ;
}
sessionKey = sessionManager.setClientSession(clientSession) ;
clientSession.setSessionKey(sessionKey) ;
clientSession.getSendQueue().sendRequest(element.getValue()) ;
try {
clientSession.waitAndGetResponse(element.getValue()) ;
} catch (WaitedLongEnoughException e) {
return Response.serverError().build() ;
} catch (UnableToResolveResponseException e) {
return Response.serverError().build() ;
}
return Response.ok(sessionKey).build();
}
}
Junit test case (removed all the HttpConnection code)
ClientIdentityConcrete clientIdentity = new ClientIdentityConcrete("username", "password", "secretkey") ;
RegistrationRequest register = new RegistrationRequest(clientIdentity);
String jsonStr = mapper.writeValueAsString(clientIdentity);
HttpPost request = new HttpPost("http://localhost:8081/mCruiseOnCarPool4All/Registration/Request");
The relevant connection is /mCruiseOnCarPool4All/carpool4all/Registration/Request
and it return a 500 error so you must have an error stacktrace on your server console.
The other URLs that you're showing are hitting 404 cause the URLs are not pointing to your Jersey servlet which seemed to be mapped to /carpool4all
Your URL pattern is :
<host>/<app>/<jerseyservlet>/<xml resource>/<method path>
with
- host = localhost:8081/ (obviously)
- app = mCruiseOnCarPool4All
- jerseyservlet = carpool4all
- xml resource = Registration
- method path = Request