Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jersey JSON Response as HttpUrlConnector instead of JSON

I'm trying to make a small REST service with Jersey 2.13 as the server, Vaadin 7.3.3 as the "client" but the idea is that the request can come from anywhere (not just Vaadin, this is why I don't use a bean at request time). Suppose that the user is already registered and the request just checks if he exists, and returns the token. I have a POST URL, https://localhost:8443/logins, that receives

{ 
    "login-request": 
    { 
        "username":<insert username>,
        "password":<insert password> 
    }
}

And returns:

{ 
    "login-token": 
    { 
        "token":<insert token>
    }
}

My client request code is:

Client client = ClientBuilder.newClient();

    WebTarget target = client.target("https://127.0.0.1:8443/").path(appName + "/logins");

    //build JSON Object
    HashMap<String, String> userMap = new HashMap<>();
    userMap.put("username", user.getUsername());
    userMap.put("password", user.getPassword());

    //JSON logins request!
    JSONObject jsonLoginRequest = new JSONObject();
    try {
        jsonLoginRequest.put("login-request", userMap);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return target.request(MediaType.APPLICATION_JSON_TYPE).post(Entity.json(jsonLoginRequest.toString()));

Server processing code is:

    @POST
@Path("logins")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response generateToken(@Context HttpServletRequest request) {

    if (request != null) {
        User user;

        DBHandshaker handshaker = DBHandshaker.getInstance();
        user = handshaker.logUser(request.getParameter("username"), request.getParameter("password"));

        if (user != null) {
            StringUtil stringUtil = StringUtil.getInstance();
            String tokenString = stringUtil.encryptForToken(user.getUsername(), user.getPassword());

            HashMap<String, String> tokenMap = new HashMap<>();
            tokenMap.put("token", tokenString);

            JSONObject jsonLoginResponse = new JSONObject();
            try {
                jsonLoginResponse.put("login-token", tokenMap);
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return Response.ok(jsonLoginResponse.toString(), MediaType.APPLICATION_JSON).build();

        } else {
            return Response.status(Response.Status.UNAUTHORIZED).build();
        }

    } else {
        return Response.status(Response.Status.NO_CONTENT).build();
    }

}

And my client response "catcher" is:

            LoginParser loginParser = new LoginParser();
        Response response = loginParser.parseRequest(username, password);

        boolean isValidLogin = Response.Status.OK.getStatusCode() == response.getStatusInfo().getStatusCode();

        if (isValidLogin) {

            // Store the current username in the service session
            getSession().setAttribute("user", username);

            HttpEntity entity = (HttpEntity) response.getEntity();
            try {
                String retSRc = EntityUtils.toString(entity);
                JSONObject jsonObject = new JSONObject(retSRc);

                System.out.println(jsonObject);

            } catch (IOException | JSONException e) {
                e.printStackTrace();
            }

            // Navigate to main view
            getUI().getNavigator().navigateTo(LoginMainView.NAME);//

        }

I'm having problems on response.getEntity() because it is neither JSON or an HttpEntity, but org.glassfish.jersey.client.HttpUrlConnector. Where is my mistake?

like image 225
Manuel Santiago Yépez Avatar asked Nov 02 '14 07:11

Manuel Santiago Yépez


1 Answers

Instead of response.getEntity(), use response.readEntity(String.class) or response.readEntity(HttpEntity.class).

like image 176
javabrett Avatar answered Oct 07 '22 22:10

javabrett