Example usage for org.apache.commons.httpclient HttpMethodBase releaseConnection

List of usage examples for org.apache.commons.httpclient HttpMethodBase releaseConnection

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpMethodBase releaseConnection.

Prototype

@Override
public void releaseConnection() 

Source Link

Document

Releases the connection being used by this HTTP method.

Usage

From source file:org.wso2.carbon.appfactory.s4.integration.utils.DomainMappingUtils.java

/**
 * Send rest request.// ww  w  .  j  a v  a  2  s  .c  o  m
 *
 * @param httpClient client object
 * @param method     method type
 * @throws AppFactoryException
 */
private static DomainMappingResponse send(HttpClient httpClient, HttpMethodBase method)
        throws AppFactoryException {
    int responseCode;
    String responseString = null;
    try {
        responseCode = httpClient.executeMethod(method);
    } catch (IOException e) {
        String msg = "Error occurred while executing method " + method.getName();
        log.error(msg, e);
        throw new AppFactoryException(msg, e);
    }
    try {
        responseString = method.getResponseBodyAsString();
    } catch (IOException e) {
        String msg = "error while getting response as String for " + method.getName();
        log.error(msg, e);
        throw new AppFactoryException(msg, e);

    } finally {
        method.releaseConnection();
    }
    if (log.isDebugEnabled()) {
        log.debug(" DomainMappingManagementService response id: " + responseCode + " message:  "
                + responseString);
    }
    return new DomainMappingResponse(responseString, responseCode);
}

From source file:org.zend.php.zendserver.deployment.core.targets.OpenShiftTargetInitializer.java

private void init() {
    try {// ww  w.j a  va 2s. c  o m
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        DeploymentCore.log(e);
    }
    HttpClient client = new HttpClient();
    HttpMethodBase method = createGetRequest(getServerUrl(), new HashMap<String, String>());
    try {
        client.executeMethod(method);
    } catch (UnknownHostException e) {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException ex) {
            DeploymentCore.log(ex);
        }
        init();
    } catch (IOException e) {
        DeploymentCore.log(e);
    } finally {
        method.releaseConnection();
    }
}

From source file:org.zend.php.zendserver.deployment.core.targets.OpenShiftTargetInitializer.java

private int executePost(String url, Map<String, String> params, Map<String, String> cookies) {
    HttpClient client = new HttpClient();
    HttpMethodBase method = createPostRequest(url, params);
    setCookies(method, cookies);/*from   w  ww .  j  av a  2  s .  co  m*/
    if (method != null) {
        try {
            return client.executeMethod(method);
        } catch (IOException e) {
            DeploymentCore.log(e);
        } finally {
            method.releaseConnection();
        }
    }
    return -1;
}

From source file:org.zend.php.zendserver.deployment.core.targets.OpenShiftTargetInitializer.java

private String executeEula(String url, Map<String, String> params) {
    HttpClient client = new HttpClient();
    HttpMethodBase method = createPostRequest(url, params);

    if (method != null) {
        int statusCode = -1;
        try {/*from w  ww  .j av a  2  s  .  c  om*/
            statusCode = client.executeMethod(method);
            if (statusCode == 302) {
                Header sessionId = method.getResponseHeader("Set-Cookie"); //$NON-NLS-1$
                String value = sessionId.getValue();
                String[] segments = value.split(";"); //$NON-NLS-1$
                for (String segment : segments) {
                    if (segment.startsWith(SESSION_ID)) {
                        String[] parts = segment.split("="); //$NON-NLS-1$
                        if (parts.length > 1) {
                            return parts[1].trim();
                        }
                    }
                }
            }
        } catch (IOException e) {
            DeploymentCore.log(e);
        } finally {
            method.releaseConnection();
        }
    }
    return null;
}

From source file:org.zend.php.zendserver.deployment.core.targets.OpenShiftTargetInitializer.java

private String executeGet(String url, Map<String, String> cookies) {
    HttpClient client = new HttpClient();
    HttpMethodBase method = createGetRequest(url, new HashMap<String, String>());
    setCookies(method, cookies);/* w  ww.jav a 2  s . c om*/
    if (method != null) {
        int statusCode = -1;
        try {
            statusCode = client.executeMethod(method);
            if (statusCode == 200) {
                String responseContent = new String(method.getResponseBody());
                return responseContent;
            }
        } catch (IOException e) {
            DeploymentCore.log(e);
        } finally {
            method.releaseConnection();
        }
        return null;
    }
    return null;
}

From source file:org.zend.php.zendserver.monitor.internal.ui.RequestGeneratorJob.java

/**
 * Create and execute HTTP request based on provided issue.
 * //w  ww  .  j a va 2 s.  c o m
 * @param zendIssue
 * @return HTTP response code
 * @throws IOException
 */
private int generate() {
    String url = zendIssue.getIssue().getGeneralDetails().getUrl();
    List<EventsGroupDetails> groups = null;
    try {
        groups = zendIssue.getGroupDetails();
    } catch (InvalidResponseException e) {
        showMessage(Messages.RequestGeneratorJob_RepeatActionTitle,
                Messages.RequestGeneratorJob_RepeatActionNotSupportedMessage);
        return -1;
    } catch (WebApiException e) {
        showMessage(Messages.RequestGeneratorJob_RepeatActionTitle,
                Messages.RequestGeneratorJob_RepeatActionFailedMessage);
        return -1;
    }
    if (groups != null && groups.size() > 0) {

        HttpClient client = new HttpClient();
        HttpMethodBase method = null;

        Event event = groups.get(0).getEvent();
        SuperGlobals globals = event.getSuperGlobals();
        if (globals != null) {
            ParameterList params = globals.getPost();
            if (params != null) {
                List<Parameter> parameters = params.getParameters();
                if (parameters != null && parameters.size() > 0) {
                    method = createPostRequest(url, parameters);
                }
            }
            params = globals.getGet();
            if (params != null) {
                if (method != null) {
                    setGetParams(method, params);
                } else {
                    method = createGetRequest(url, params);
                }
            }
            params = globals.getCookie();
            if (method != null && params != null) {
                setCookies(method, params);
            }
        }

        if (method != null) {
            int statusCode = -1;
            try {
                statusCode = client.executeMethod(method);
            } catch (IOException e) {
                Activator.log(e);
            } finally {
                method.releaseConnection();
            }
            return statusCode;
        }
    }
    return -1;
}

From source file:org.zend.sdklib.internal.target.ApiKeyDetector.java

private boolean isBootstrapped() throws SdkException {
    HttpClient client = new HttpClient();
    HttpMethodBase method = new GetMethod(getUrl("/Login")); //$NON-NLS-1$
    if (method != null) {
        int statusCode = -1;
        try {/*from   w  w  w .  ja  v  a  2s .c o  m*/
            statusCode = client.executeMethod(method);
            if (statusCode == 200) {
                String responseContent = new String(method.getResponseBody());
                if (!responseContent.contains("BootstrapWizard")) { //$NON-NLS-1$
                    return true;
                }
            }
        } catch (IOException e) {
            throw new SdkException(e);
        } finally {
            method.releaseConnection();
        }
    }
    return false;
}

From source file:org.zend.sdklib.internal.target.ApiKeyDetector.java

private String executeLogin(String url, Map<String, String> params) throws SdkException {
    HttpClient client = new HttpClient();
    HttpMethodBase method = createPostRequest(url, params);
    if (method != null) {
        int statusCode = -1;
        try {//w  w w. j a v  a  2s. co  m
            statusCode = client.executeMethod(method);
            if (statusCode == 302) {
                Header sessionId = method.getResponseHeader("Set-Cookie"); //$NON-NLS-1$
                String value = sessionId.getValue();
                String[] segments = value.split(";"); //$NON-NLS-1$
                String currentValue = null;
                for (String segment : segments) {
                    String[] parts = segment.split(","); //$NON-NLS-1$
                    for (String part : parts) {
                        if (part.trim().startsWith(SESSION_ID)) {
                            String[] id = part.split("="); //$NON-NLS-1$
                            if (id.length > 1) {
                                currentValue = id[1].trim();
                            }
                        }
                    }
                }
                return currentValue;
            } else if (statusCode == 200) {
                throw new InvalidCredentialsException();
            }
        } catch (IOException e) {
            throw new SdkException(e);
        } finally {
            method.releaseConnection();
        }
    }
    return null;
}

From source file:org.zend.sdklib.internal.target.ApiKeyDetector.java

private String executeAddApiKey(String url, Map<String, String> params, Map<String, String> cookies)
        throws SdkException {
    HttpClient client = new HttpClient();
    HttpMethodBase method = createPostRequest(url, params);
    setCookies(method, cookies);/* w w w.j  a va2 s.  c  om*/
    method.setRequestHeader("X-Accept", //$NON-NLS-1$
            "application/vnd.zend.serverapi+json;version=1.3;q=1.0"); //$NON-NLS-1$
    method.setRequestHeader("X-Request", "JSON"); //$NON-NLS-1$ //$NON-NLS-2$
    if (method != null) {
        int statusCode = -1;
        try {
            statusCode = client.executeMethod(method);
            if (statusCode == 200) {
                String responseContent = new String(method.getResponseBody());
                return responseContent;
            } else if (statusCode == 500) {
                String val = params.remove(NAME);
                params.put(NAME, val + new Random().nextInt());
                return executeAddApiKey(url, params, cookies);
            }
        } catch (IOException e) {
            throw new SdkException(e);
        } finally {
            method.releaseConnection();
        }
    }
    return null;
}

From source file:org.zend.sdklib.internal.target.ApiKeyDetector.java

private String executeGetApiKeys(String url, Map<String, String> cookies) throws SdkException {
    HttpClient client = new HttpClient();
    HttpMethodBase method = createGetRequest(url, new HashMap<String, String>());
    setCookies(method, cookies);/*from   w  w  w .j  ava 2s  .  c om*/
    method.setRequestHeader("X-Accept", //$NON-NLS-1$
            "application/vnd.zend.serverapi+json;version=1.3;q=1.0"); //$NON-NLS-1$
    method.setRequestHeader("X-Request", "JSON"); //$NON-NLS-1$ //$NON-NLS-2$
    if (method != null) {
        int statusCode = -1;
        try {
            statusCode = client.executeMethod(method);
            if (statusCode == 200) {
                String responseContent = new String(method.getResponseBody());
                return responseContent;
            }
        } catch (IOException e) {
            throw new SdkException(e);
        } finally {
            method.releaseConnection();
        }
    }
    return null;
}