Example usage for org.apache.http.impl.client DefaultHttpClient close

List of usage examples for org.apache.http.impl.client DefaultHttpClient close

Introduction

In this page you can find the example usage for org.apache.http.impl.client DefaultHttpClient close.

Prototype

public void close() 

Source Link

Usage

From source file:org.wso2.carbon.dynamic.client.web.proxy.RegistrationProxy.java

@DELETE
@Produces(MediaType.APPLICATION_JSON)//from ww w.j  a v  a2  s. c om
public Response unregister(@QueryParam("applicationName") String applicationName,
        @QueryParam("userId") String userId, @QueryParam("consumerKey") String consumerKey) {
    Response response;
    DefaultHttpClient httpClient = DCRProxyUtils.getHttpsClient();
    String host = DCRProxyUtils.getKeyManagerHost();
    try {
        URI uri = new URIBuilder().setScheme(Constants.RemoteServiceProperties.DYNAMIC_CLIENT_SERVICE_PROTOCOL)
                .setHost(host).setPath(Constants.RemoteServiceProperties.DYNAMIC_CLIENT_SERVICE_ENDPOINT)
                .setParameter("applicationName", applicationName).setParameter("userId", userId)
                .setParameter("consumerKey", consumerKey).build();
        HttpDelete httpDelete = new HttpDelete(uri);
        CloseableHttpResponse serverResponse = httpClient.execute(httpDelete);
        HttpEntity responseData = serverResponse.getEntity();
        int status = serverResponse.getStatusLine().getStatusCode();
        String resp = EntityUtils.toString(responseData, Constants.CharSets.CHARSET_UTF_8);
        response = Response.status(DCRProxyUtils.getResponseStatus(status)).entity(resp).build();
    } catch (URISyntaxException e) {
        String msg = "Server error occurred while deleting the client '" + applicationName + "'";
        log.error(msg, e);
        response = Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
    } catch (UnsupportedEncodingException e) {
        String msg = "Request data encoding error occurred while deleting the client '" + applicationName + "'";
        log.error(msg, e);
        response = Response.status(Response.Status.UNSUPPORTED_MEDIA_TYPE).entity(msg).build();
    } catch (IOException e) {
        String msg = "Service invoke error occurred while deleting the client '" + applicationName + "'";
        log.error(msg, e);
        response = Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
    } finally {
        httpClient.close();
    }
    return response;
}

From source file:org.wso2.carbon.dynamic.client.web.proxy.RegistrationProxy.java

@POST
@Consumes(MediaType.APPLICATION_JSON)//from   w  w  w  .  j  a  v a  2s.c  om
@Produces(MediaType.APPLICATION_JSON)
public Response register(RegistrationProfile registrationProfile) {
    DefaultHttpClient httpClient = DCRProxyUtils.getHttpsClient();
    String host = DCRProxyUtils.getKeyManagerHost();
    Response response;
    try {
        URI uri = new URIBuilder().setScheme(Constants.RemoteServiceProperties.DYNAMIC_CLIENT_SERVICE_PROTOCOL)
                .setHost(host).setPath(Constants.RemoteServiceProperties.DYNAMIC_CLIENT_SERVICE_ENDPOINT)
                .build();
        Gson gson = new Gson();
        StringEntity entity = new StringEntity(gson.toJson(registrationProfile), MediaType.APPLICATION_JSON,
                Constants.CharSets.CHARSET_UTF_8);
        HttpPost httpPost = new HttpPost(uri);
        httpPost.setEntity(entity);
        CloseableHttpResponse serverResponse = httpClient.execute(httpPost);
        HttpEntity responseData = serverResponse.getEntity();
        int status = serverResponse.getStatusLine().getStatusCode();
        String resp = EntityUtils.toString(responseData, Constants.CharSets.CHARSET_UTF_8);
        response = Response.status(DCRProxyUtils.getResponseStatus(status)).entity(resp).build();
    } catch (URISyntaxException e) {
        String msg = "Server error occurred while registering client '" + registrationProfile.getClientName()
                + "'";
        log.error(msg, e);
        response = Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
    } catch (UnsupportedEncodingException e) {
        String msg = "Request data encoding error occurred while registering client '"
                + registrationProfile.getClientName() + "'";
        log.error(msg, e);
        response = Response.status(Response.Status.UNSUPPORTED_MEDIA_TYPE).entity(msg).build();
    } catch (IOException e) {
        String msg = "Service invoke error occurred while registering client.";
        log.error(msg, e);
        response = Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
    } finally {
        httpClient.close();
    }
    return response;
}

From source file:org.auraframework.util.perfomance.PTestGoogleChart.java

/**
 * Write the chart to the specified file. If the file already exists, it
 * will be replaced./*from ww  w  .j a  v a 2  s.  co m*/
 * 
 * @param file Write to this file.
 * @return Whether the file was successfully created.
 */
@SuppressWarnings("deprecation")
public boolean writeToFile(File file) throws IOException {

    HttpParams httpParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParams, 24000);
    HttpConnectionParams.setSoTimeout(httpParams, 24000);

    DefaultHttpClient http = new DefaultHttpClient(httpParams);

    HttpPost post = new HttpPost(BASE_URL);

    List<NameValuePair> nvps = new ArrayList<>();

    Map<String, String> params = buildRequestParams();
    for (Map.Entry<String, String> entry : params.entrySet()) {
        nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
    }
    post.setEntity(new UrlEncodedFormEntity(nvps));

    post.setHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded; charset=UTF-8");

    try {
        HttpResponse response = http.execute(post);

        InputStream in = null;
        FileOutputStream fw = null;
        ByteArrayOutputStream baos = null;

        boolean successful = false;
        int responseCode = response.getStatusLine().getStatusCode();
        try {
            if (responseCode == 200) {
                if (file.exists()) {
                    file.delete();
                }
                file.createNewFile();

                in = new BufferedInputStream(response.getEntity().getContent());
                baos = new ByteArrayOutputStream();
                fw = new FileOutputStream(file);

                IOUtil.copyStream(in, baos);
                byte[] bytes = baos.toByteArray();
                fw.write(bytes);
                successful = true;
            } else {
                System.out.println(response.getEntity().getContent());
                throw new RuntimeException("Callout to Google Charts API failed.");
            }
        } finally {
            post.releaseConnection();
            if (in != null) {
                in.close();
            }
            if (fw != null) {
                fw.close();
            }
            if (baos != null) {
                baos.close();
            }
        }

        return successful;
    } finally {
        http.close();
    }
}