Example usage for org.apache.commons.httpclient URIException getStackTrace

List of usage examples for org.apache.commons.httpclient URIException getStackTrace

Introduction

In this page you can find the example usage for org.apache.commons.httpclient URIException getStackTrace.

Prototype

public StackTraceElement[] getStackTrace() 

Source Link

Document

Provides programmatic access to the stack trace information printed by #printStackTrace() .

Usage

From source file:data.engine.request.builder.RequestBuilder.java

private String EncodeQuery(String query) {
    String str = "";
    try {/*from  w w w . java 2  s. c  o  m*/
        str = URIUtil.encodeQuery(query);
    } catch (URIException ex) {
        System.out.println("Unable to encode request query " + ex.getStackTrace());
    }
    return str;
}

From source file:io.cortical.services.api.client.ApiInvoker.java

/** 
* Serialize an Object./*from   w w w  .  j  a v a  2 s  .  co  m*/
* @param host the targeted host
* @param path the targeted rest endpoint
* @param method the HTTP method
* @param queryParams the query parameters
* @param body the obligatory body of a post
* @param headerParams the HTTP header parameters
* @param contentType the content type
* @throws APIException if an exception occurs during querying of the API.
**/
public Object invokeAPI(String host, String path, String method, Map<String, String> queryParams, Object body,
        Map<String, String> headerParams, String contentType) throws ApiException {
    Client client = getClient(host);

    StringBuilder b = new StringBuilder();

    for (String key : queryParams.keySet()) {
        String value = queryParams.get(key);
        if (value != null) {
            if (b.toString().length() == 0)
                b.append("?");
            else
                b.append("&");
            b.append(escapeString(key)).append("=").append(escapeString(value));
        }
    }
    String querystring = b.toString();

    try {
        querystring = URIUtil.encodeQuery(querystring);
    } catch (URIException e) {
        throw new ApiException(0, e.getStackTrace().toString());
    }

    Builder builder = client.resource(host + path + querystring)
            .accept(new String[] { "application/json", "image/png" });
    for (String key : headerParams.keySet()) {
        builder.header(key, headerParams.get(key));
    }

    for (String key : defaultHeaderMap.keySet()) {
        if (!headerParams.containsKey(key)) {
            builder.header(key, defaultHeaderMap.get(key));
        }
    }
    ClientResponse response = null;

    if ("GET".equals(method)) {
        response = (ClientResponse) builder.get(ClientResponse.class);
    } else if ("POST".equals(method)) {
        if (body == null)
            response = builder.post(ClientResponse.class, serialize(body));
        else
            response = builder.type("application/json").post(ClientResponse.class, serialize(body));
    } else if ("PUT".equals(method)) {
        if (body == null)
            response = builder.put(ClientResponse.class, serialize(body));
        else
            response = builder.type("application/json").put(ClientResponse.class, serialize(body));
    } else if ("DELETE".equals(method)) {
        if (body == null)
            response = builder.delete(ClientResponse.class, serialize(body));
        else
            response = builder.type("application/json").delete(ClientResponse.class, serialize(body));
    } else {
        throw new ApiException(500, "unknown method type " + method);
    }
    if (response.getClientResponseStatus() == ClientResponse.Status.NO_CONTENT) {
        return null;
    } else if (response.getClientResponseStatus().getFamily() == Family.SUCCESSFUL) {
        // Handle the casting of the response based on the type.
        if (!response.getHeaders().get("Content-Type").get(0).equals(MediaType.APPLICATION_JSON)) {
            return response.getEntityInputStream();
        }
        return (String) response.getEntity(String.class);
    } else {
        throw new ApiException(response.getClientResponseStatus().getStatusCode(),
                response.getEntity(String.class));
    }
}

From source file:io.cortical.retina.rest.ApiInvoker.java

/** 
* Serialize an Object./*from  www .j av a2 s .  co  m*/
* @param host the targeted host
* @param path the targeted rest endpoint
* @param method the HTTP method
* @param queryParams the query parameters
* @param body the obligatory body of a post
* @param headerParams the HTTP header parameters
* @param contentType the content type
* @throws APIException if an exception occurs during querying of the API.
**/
public Object invokeAPI(String host, String path, String method, Map<String, String> queryParams, Object body,
        Map<String, String> headerParams, String contentType) throws ApiException {
    Client client = getClient(host);

    headerParams.put("api-client", "java_1.0.0");
    StringBuilder b = new StringBuilder();

    for (String key : queryParams.keySet()) {
        String value = queryParams.get(key);
        if (value != null) {
            if (b.toString().length() == 0)
                b.append("?");
            else
                b.append("&");
            b.append(escapeString(key)).append("=").append(escapeString(value));
        }
    }
    String querystring = b.toString();

    try {
        querystring = URIUtil.encodeQuery(querystring);
    } catch (URIException e) {
        throw new ApiException(0, e.getStackTrace().toString());
    }

    Builder builder = client.resource(host + path + querystring)
            .accept(new String[] { "application/json", "image/png" });
    for (String key : headerParams.keySet()) {
        builder.header(key, headerParams.get(key));
    }

    for (String key : defaultHeaderMap.keySet()) {
        if (!headerParams.containsKey(key)) {
            builder.header(key, defaultHeaderMap.get(key));
        }
    }
    ClientResponse response = null;

    if ("GET".equals(method)) {
        response = (ClientResponse) builder.get(ClientResponse.class);
    } else if ("POST".equals(method)) {
        if (body == null)
            response = builder.post(ClientResponse.class, serialize(body));
        else
            response = builder.type("application/json").post(ClientResponse.class, serialize(body));
    } else if ("PUT".equals(method)) {
        if (body == null)
            response = builder.put(ClientResponse.class, serialize(body));
        else
            response = builder.type("application/json").put(ClientResponse.class, serialize(body));
    } else if ("DELETE".equals(method)) {
        if (body == null)
            response = builder.delete(ClientResponse.class, serialize(body));
        else
            response = builder.type("application/json").delete(ClientResponse.class, serialize(body));
    } else {
        throw new ApiException(500, "unknown method type " + method);
    }
    if (response.getClientResponseStatus() == ClientResponse.Status.NO_CONTENT) {
        return null;
    } else if (response.getClientResponseStatus().getFamily() == Family.SUCCESSFUL) {
        // Handle the casting of the response based on the type.
        if (!response.getHeaders().get("Content-Type").get(0).equals(MediaType.APPLICATION_JSON)) {
            return response.getEntityInputStream();
        }
        return (String) response.getEntity(String.class);
    } else {
        throw new ApiException(response.getClientResponseStatus().getStatusCode(),
                response.getEntity(String.class));
    }
}