Example usage for org.apache.commons.httpclient.methods StringRequestEntity getContentLength

List of usage examples for org.apache.commons.httpclient.methods StringRequestEntity getContentLength

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.methods StringRequestEntity getContentLength.

Prototype

public long getContentLength() 

Source Link

Usage

From source file:org.apache.wookie.util.gadgets.GadgetUtils.java

/**
 * Returns metadata from a metadata service for the requested gadget url.
 * If the request successfully completes, but no valid gadget is found, then
 * no exception is thrown - the error information will instead be returned in the JSON string.
 * @param service//from   w  w w. ja  v a2s .  c o m
 * @param url
 * @return Gadget metadata as a JSON-formatted String; this may include either gadgets or errors depending on the outcome of the request
 * @throws Exception if the service does not exist, the service responds with a HTTP error, or the widget URL is malformed
 */
public static String getMetadata(String service, String url) throws Exception {

    System.out.println("getMetadata():service=" + service + "\turl=" + url);

    try {
        new URL(url);
    } catch (MalformedURLException m) {
        throw new Exception("Gadget URL was malformed");
    }

    try {
        String request = "{\"context\":{\"country\":\"US\",\"language\":\"en\",\"view\":\"home\",\"container\":\""
                + CONTAINER_ID + "\"},\"gadgets\":[{\"url\":\"" + url + "\",\"moduleId\":1}]}";
        System.out.println("request string=" + request);
        StringRequestEntity req = null;
        req = new StringRequestEntity(request, "text/x-json", "UTF-8");
        System.out.println("going to post to service");

        //PostMethod post = new PostMethod(service);
        PostMethod post = new PostMethod("http://localhost:12345/gadgets/metadata/");

        post.setRequestEntity(req);
        post.setRequestHeader("Content-Length", String.valueOf(req.getContentLength()));
        return executeMethod(post);
    } catch (Exception e) {
        throw new Exception("There was a problem connecting to the Shindig metadata service");
    }

}