Validation of HTTP Header with Javax.ws - java

I am a little bit frustrated. I do not know how I can validate the http header. So let me give you a little bit of background:
I have an android app. The android app is calling my web service and the web service is handling the connection with the database.
I am sending a token in the header of my app to the web service. In addition to that I am sending the data in a JSON format. So for instance my app will send something like that
Header
token: xyz
{"username":"abc","postMessage":"hello world"}
In the webservice I want to validate the token. So I created this method `
public String headerInfo(#Context HttpHeaders httpHeaders){
String token = httpHeaders.getRequestHeader("token").get(0);
return cacheControl.toString(); }
In my other method I am calling this method, but of I am not able to add the right parameters here, so I am receiving NULL as a response.
My method looks (for testing purpose) like that:
#GET
#Path("/validate")
public Response validation(String json){
//... get username and post from the json object, for testing I added the key token as well...//
String token = jsonObj.getString("token");
String headerToken = headerInfo();
//...compare token and headerToken...//
return Response... }
Thanks,
Jan

it was pretty easy in the end. Maybe it will help someone.
I just needed to add the HeaderParam in my method and I could access it.
#HeaderParam("content-type") String ct

Related

Rest assured testing - API request 401 bad request

I have a problem with sending API request via postman or Java lib "io.restassured".
When I do the same action on UI the request returns correct response, but when I try the same thing via postman or java code I get:
401 Bad request Your browser sent an invalid
request.
The java code
public static void main(String[] args) {
String requestUrl = "exampleBaseUrl/app/reports/api/rest/v1/graphs?context=shipper&reports_type=freights";
Response response = RestAssured.given().relaxedHTTPSValidation().header("x-csrf-token", "18ea65e740eb0ddddadf0ef435d92564").
when().
get(requestUrl);
}
I assume something is wrong with the authentication, because in dev tools i can see a Get request for CSRF_token, and it looks like this:
the endpoint for the token:
/login?get_csrf_token
and for this request I get following response:
{"csrf_token":"18ea65e740eb0ddddadf0ef435d92564"}
I am not sure how to solve this, I have also tried to get the token via java code by sending a get request to the token's endpoint /login?get_csrf_token
and this one gets my a HTML response with empty username and password input.
Error 401 means your request isn't authorized.
For authorization, usually while logging in you are given a token, which you will have to keep in your cache/local-memory and whenever you communicate with the server you have to add that in your request header (for your own introduction to the server)
It seems like in your case you can get a token from /login?get_csrf_token after logging in. Note that you don't need authorization for a login service.
Now, after getting token from the server, how to add it as a request header? See REST Assured Documentation

Spring MVC Request method 'GET' not supported

I have found few questions on this topic however it does not necessary answer my question
Basicaly
I am passing some values via url so data can be gathered from database. I can do it via method= RequestMethod.GET however I would like to do it via POST so users doesnt see parameters in URL.
I am not sure i am using the best method, i bet there is something much advance in order to achieve this .
Cotroller class
#RequestMapping(value="/empresa", method= RequestMethod.POST)
public String empresa(Model model, Principal principal, #RequestParam("get_Business_ID") String get_Business_ID){
// get selected business
List<Business> selectedBusiness = businessService.getBusinessByBusinessID(get_Business_ID);
System.out.println("business selected= "+ selectedBusiness.get(0).getBusiness_name());
model.addAttribute("selectedBusiness",selectedBusiness);
//Destaque semanal
List<Business> businessList = businessService.getCurrentBusiness();
model.addAttribute("businessList", businessList);
return "empresa";
}
JSP page link
href="${pageContext.request.contextPath}/empresa?get_Business_ID=${business.business_id}"
error Type Status Report
Message Request method 'GET' not supported
Description The method received in the request-line is known by the
origin server but not supported by the target resource.
maybe RequestMethod.GET only works if i am using a form with post method?
Is there any other way to achieve this?
Thanks in advance
You have annotated your method with POST
#RequestMapping(value="/empresa", method= RequestMethod.POST)
So change this to
#RequestMapping(value="/empresa", method= RequestMethod.GET)
If you want it to be a POST request try form submit instead of href
Still you need href? then try this
Make a link use POST instead of GET
You are facing this issue because browser never sends anything, browser only receives requests and forwards them to your back end logic.
Difference between GET is simple fetching of data without any data from your side and POST is in information that you send with your request ( for example you send data that you need to save specific customer, I need this firstName, lastName, email etc to be saved ). With that being said, you can switch between #GetMapping or #PostMapping depending what you need for your application.
To be more precise between get and post
GET A GET method should be used to retrieve data from the server. Multiple get requests to the same URL should be valid and no data should be changed on the server side.
However, this doesn't mean it is not possible to make a GET request change things server side, but you should try to make sure you are following the standard.
POST A POST method should be used when you need to create, update or delete data on the server side. Making the same POST request multiple times may not be safe and may result in inconsistent data. The content of a POST request is sent in the request body. Hence, you don't see the parameters in your browser, but it is easy to see them if you wanted to (Even using the browser developer tools) so it is no more safe than a GET request.

JUnit for REST: Cannot POST data

I want to write a JUnit class for a REST endpoint.
This is my REST method. It works fine.
#POST
#Path("create")
#Produces(APPLICATION_JSON)
public String create(#QueryParam("parentId") String parentId, #QueryParam("name") String name) {
//do sth.
return "{\"status\": \"SUCCESS\"}";
}
Now my JUnit test looks like that, which doesn't work, because I don't know how to POST my data in the right way:
#Test
public void testCreate() {
Client client = ClientBuilder.newClient();
WebTarget wt = client.target(REST_MENU_URL + "create");
String queryParams = "parentId=1&name=NEW_JUnit_NEW";
// In the line below, I want to POST my query parameters, but I do it wrong
Response response = wt.request().post(Entity.entity(queryParams, APPLICATION_JSON), Response.class);
// The response has a 500, because the query parameters are all NULL!
assertEquals("Http code should be 200", 200, response.getStatus());
}
So how do I have to change the line with the 'Response' to make it work?
The problem is, that the query parameters (parentId and name) don't get transmitted (response = wt.request().post(...)).
I tried to POST form parameters too, but no success here either. Just like that:
Form form =new Form().param("parentId", "4").param("name", "NEW_JUnit_NEW");
Response response = wt.request().post(Entity.entity(form, APPLICATION_JSON), Response.class);
Thanks,
Bernhard
Check out the Jersey Client documentation, in particular section 5.3.4 on targeting resources.
Query parameters form a part of the URI of the resource, they're not part of the body of the document posted to the resource. You're seeing null in your resource because you're not filling in the query parameters in the URI, you're posting them as the body. You need to tell Jersey to put them in the URI...
WebTarget wt = client.target(REST_MENU_URL + "create").queryParam("parentId", 1).queryParam("name", "NEW_JUnit_NEW");
You'll also need to ensure that your POST request sets the Accept header to allow application/json (by calling the accept(...) method after calling request()) and you're going to need to construct some kind of Entity to pass to the post(...) method - the problem here is that your resource is not consuming the entity body but the client API expects you to send something - this is a code smell which suggests your API is not particularly ReSTful. You can probably get away with some kind of empty body constructed from an empty string. It should look a bit like this...
Response response = wt.request().accept(MediaType.APPLICATION_JSON).post(Entity.text(""))
Alternatively, you could look into converting your API so that it accepts a JSON document and move the query parameters into that document.

Error while calling RESTful Web-Service using Jersey Client POST method

Getting an error while trying to consume a Restful web service using
POST method(with form param).
I want to consume a REST application using POST method.
Please find below the resource class I want to access.
#Path("/user")
public class User {
#POST
#Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response get(#FormParam("username") String userName,
#FormParam("userid") String userId ){
}
I tried using Jesry Client for accessing.Please find below the code i tried.
I tried adding values to FormParam as shown below.
Trail 1
WebResource webResource = client.resource("baseURL/user");
String input = "userid:1001,username:demo1";
ClientResponse response = webResource.type("application/x-www-form-urlencoded").post(ClientResponse.class, input);
I am getting a an error response back "The server encountered an internal error () that prevented it from fulfilling this request".
I think I am not adding the values to FormParam properly.
Trial 2
I also tried adding the form params using the below code
MultivaluedMap formData = new MultivaluedMapImpl();
formData.add("userid", "1001");
formData.add("username", "demo1");
ClientResponse response = webResource.type("application/x-www-form-urlencoded").post(ClientResponse.class, formData);
This also resulted in the same error.
Trial 3
Form f = new Form();
f.add("userid", "1001D");
f.add("username", "1001D");
ClientResponse response = webResource.type(MediaType.APPLICATION_FORM_URLENCODED_TYPE).post(ClientResponse.class, f);
This also resulted in the same error.
Any help is appreciated.
Since your error indicates "Server encountered an internal error" you need to look at the server (logs) to see what went wrong. Certainly your 3rd client looks fine to reach the service you defined (assuming you are using something real instead of the string "baseURL").
You can easily test your server is working separately from your client by creating a HTML page to reach the service. Create a HTML form using enctype="application/x-www-form-urlencoded" and posting to your service endpoint (what you are calling "baseURL/user") with form parameters userid and username. When you view the HTML form in a browser and hit the submit button, it will call your server - if you get the same error you can be sure it is nothing to do with your client code.
Hope http://yogeshmprajapati.blogspot.in/2011/12/login-to-fb-from-java.html will help you.

Using RestEasy with Windows Live Service, how do you unmarshall the list of Contacts returned?

I'm trying to get my contacts from Windows Live using RestEasy
After succesfully authenticating my user, I've made the call to https://livecontacts.services.live.com/users/#L#/rest/livecontacts
Set the authentication header, added my id and my tokens
If i make the call from command line using cUrl I get the expected output, but in my web application I'm getting back gibberish
e.g.
...?{?[??e^7E?...
Current interface class is
public interface WindowsLiveAPI {
#GET
#Path(value="/#L#{liveId}/rest/livecontacts")
Response getContacts(#PathParam("liveId") #Encoded String liveId, #HeaderParam("Authorization") String delegatedToken);
}
ThrowAway test:
ResteasyProviderFactory.getInstance().addMessageBodyReader(DefaultTextPlain.class);
RegisterBuiltin.register(ResteasyProviderFactory.getInstance());
WindowsLiveAPI client = ProxyFactory.create(WindowsLiveAPI.class, "https://livecontacts.services.live.com");
ClientResponse<LiveContacts> response = (ClientResponse) client.getContacts(LIVE_ID, DELEGATED_TOKEN);
System.out.println(response.getStatus()); //Produces 200 (401 after token expires)
System.out.println(response.getEntity(String.class)); //produces gibberish
Does anyone have any clue how to unmarshal the response
You could try #Produces(MediaType.APPLICATION_XML) [if it's XML] on the method.

Categories