1

I am creating a Java EE client where I need to make a call to a node (js) server to get the response. So I made a single class through which requests are made to node server. Everytime I get a response, I need to send back the response code and the response itself. So I thought of creating a String array which would contain the response code and the response.

Here is my class:

import javax.ws.rs.core.MediaType;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.client.filter.LoggingFilter;
import com.sun.jersey.core.impl.provider.entity.StringProvider;

public class RequestHandler {

/**
 * Makes a HTTP Request for a given url. The type
 * determines which type of request, post or get,
 * will be made. The params cane be in form of
 * name=value pair or JSON format.
 * 
 * @param type Request method. 1 - Get, 2 - Post.
 * @param url url string to which request has to be 
 * made.
 * @param path path of the resource or service for a
 * url.
 * @param params request parameters. Can be either
 * name=value pair or JSON request.
 * 
 * @return String representation of the response.
 * 
 */
public static String[] makeRequest(int type, String url, String path, String params) {
    String[] response = new String[2];
    ClientResponse clientResponse = null;
    try {
        ClientConfig config = new DefaultClientConfig();
        config.getClasses().add(StringProvider.class); 
        Client client = Client.create(config);

        WebResource service =
                client.resource(url); 
        client.addFilter(new LoggingFilter());
        service.path("rest");

        // 1 - GET, 2 - POST
        switch (type) {
            case 1: {
                System.out.println("Making GET request to: " + url + path);
                System.out.println("Request Params: " + params);
                clientResponse = service.path(path).type(MediaType.APPLICATION_JSON_TYPE).accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); //TODO Code to be corrected, include params
                break;
            }
            case 2: {
                System.out.println("Making POST request to: " + url + path);
                System.out.println("Request Params: " + params);
                clientResponse = service.path(path).type(MediaType.APPLICATION_JSON_TYPE).accept(MediaType.APPLICATION_JSON).post(ClientResponse.class, params);
                break;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        response[0] = "500";
        response[1] = "Internal Server Error";
        return response;
    }

    response[0] = String.valueOf(clientResponse.getStatus());
    response[1] = clientResponse.getEntity(String.class);

    System.err.println("Response status: " + response[0]);

    return response;
    }//end of makeRequest()

}//end of class

But I am not convinced by creating too much string objects which would cause performance issues. So I thought of creating a Map which would send back the response code and the response.

response.put(500, "Internal Server Error");

But again, creating a map with integer and checking everytime the response code creates overhead of boxing and unboxing of Integer object, which again could result in performance.

HashMap<Integer, String> response = RequestHandler.makeRequest(2, urlString, "/login", params);

if (response.containsKey(500)) {
 return Message.INTERNAL_SERVER_ERROR;
}

Which one should I use for better performance? Or is there any better alternative out of it?

4

3 回答 3

3

Given that a HashMap contains an array, plus a bunch of housekeeping items (size, threshold, load factor), a String[] array is going to be smaller (and thus, by one measure, more efficient) than a HashMap.

However, avoid premature optimisation.

于 2012-05-09T05:09:28.243 回答
1

I don't think it makes much of a performance difference either way. But for clarity I would suggest using a Enums or static final variables since you already know all the possible codes before hand. A HashMap would be good if you needed to add/remove data dynamically but that doesn't apply in this case.

于 2012-05-09T05:10:20.147 回答
0

I think that you can define your own response object like this:

class Response{
   int status;
   String msg;
}
于 2012-05-09T05:10:47.753 回答