Example usage for org.apache.commons.httpclient HttpException getMessage

List of usage examples for org.apache.commons.httpclient HttpException getMessage

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpException getMessage.

Prototype

public String getMessage() 

Source Link

Document

Returns the detail message string of this throwable.

Usage

From source file:org.sipfoundry.sipxconfig.admin.alarm.AlarmHistoryManagerImpl.java

private InputStream getAlarmDataStream(String host) throws IOException {
    GetMethod httpget = null;//from   w  w w  . j a  v  a 2  s .co m
    try {
        HttpClient client = new HttpClient();
        Location location = m_locationsManager.getLocationByFqdn(host);
        httpget = new GetMethod(location.getHttpsServerUrl() + m_logDirectory + "/" + ALARMS_LOG);
        int statusCode = client.executeMethod(httpget);
        if (statusCode != 200) {
            throw new UserException("&error.https.server.status.code", host, String.valueOf(statusCode));
        }
        byte[] bytes = httpget.getResponseBody();
        return new ByteArrayInputStream(bytes);
    } catch (HttpException ex) {
        throw new UserException("&error.https.server", host, ex.getMessage());
    } catch (XmlRpcRemoteException ex) {
        throw new UserException("&error.xml.rpc", ex.getMessage(), host);
    } finally {
        if (httpget != null) {
            httpget.releaseConnection();
        }
    }
}

From source file:org.sipfoundry.sipxconfig.admin.CertificateManagerImpl.java

public void deleteCA(CertificateDecorator cert) {
    if (isSystemGenerated(cert.getFileName())) {
        throw new UserException("&error.delete.default.ca", cert.getFileName());
    }//from   w  ww  . j a  v a2  s  . c o m

    Collection<File> files = FileUtils.listFiles(new File(m_sslAuthDirectory), SSL_CA_EXTENSIONS, false);
    Collection<File> filesToDelete = new ArrayList<File>();
    try {
        for (File file : files) {
            if (StringUtils.equals(file.getCanonicalFile().getName(), cert.getFileName())) {
                filesToDelete.add(getCAFile(file.getName()));
            }
        }
        HttpClient httpClient = getNewHttpClient();
        Location[] locations = m_locationsManager.getLocations();
        for (Location curLocation : locations) {
            for (File file : filesToDelete) {
                String remoteFileName = join(
                        new Object[] { curLocation.getHttpsServerUrl(), file.getAbsolutePath() });
                DeleteMethod httpDelete = new DeleteMethod(remoteFileName);
                try {
                    int statusCode = httpClient.executeMethod(httpDelete);
                    if (statusCode != 200) {
                        throw new UserException("&error.https.server.status.code", curLocation.getFqdn(),
                                String.valueOf(statusCode));
                    }
                } catch (HttpException ex) {
                    throw new UserException("&error.https.server", curLocation.getFqdn(), ex.getMessage());
                } finally {
                    httpDelete.releaseConnection();
                }
            }
        }
    } catch (IOException ex) {
        throw new UserException("&error.delete.cert");
    }
}

From source file:org.sipfoundry.sipxconfig.vm.MailboxManagerImpl.java

private void invokeWebservice(String uri) {
    PutMethod httpPut = null;/* w w w .  j  a  v  a  2  s .  com*/
    String host = null;
    String port = null;
    SipxIvrService ivrService = (SipxIvrService) m_sipxServiceManager
            .getServiceByBeanId(SipxIvrService.BEAN_ID);

    Location ivrLocation = (Location) singleResult(m_locationsManager.getLocationsForService(ivrService));
    if (ivrLocation == null) {
        throw new UserException("&voicemail.install.error");
    }
    try {
        HttpClient client = new HttpClient();
        host = ivrLocation.getFqdn();
        port = ivrService.getHttpsPort();
        httpPut = new PutMethod(getMailboxServerUrl(host, Integer.parseInt(port)) + uri);
        int statusCode = client.executeMethod(httpPut);
        if (statusCode != 200) {
            throw new UserException("&error.https.server.status.code", host, String.valueOf(statusCode));
        }
    } catch (HttpException ex) {
        throw new UserException("&error.https.server", host, ex.getMessage());
    } catch (IOException ex) {
        throw new UserException("&error.io.exception", host, ex.getMessage());
    } finally {
        if (httpPut != null) {
            httpPut.releaseConnection();
        }
    }
}

From source file:org.sonatype.nexus.restlight.common.AbstractRESTLightClient.java

/**
 * <p>//from  w  w w .  j av  a 2  s .c  o  m
 * Low-level GET implementation.
 * </p>
 * <p>
 * Submit a GET request to the URL given (absolute or relative-to-base-URL depends on urlIsAbsolute flag), and parse
 * the response as an XML {@link Document} (JDOM) instance. Use the given requestParams map to inject into the HTTP
 * GET method.
 * </p>
 */
@SuppressWarnings("unchecked")
protected Document get(final String url, final Map<String, ? extends Object> requestParams,
        final boolean urlIsAbsolute) throws RESTLightClientException {
    GetMethod method = urlIsAbsolute ? new GetMethod(url) : new GetMethod(formatUrl(baseUrl, url));
    method.addRequestHeader("Content-Type", "application/xml");

    addRequestParams(method, requestParams);

    try {
        client.executeMethod(method);
    } catch (HttpException e) {
        throw new RESTLightClientException("GET request execution failed. Reason: " + e.getMessage(), e);
    } catch (IOException e) {
        throw new RESTLightClientException("GET request execution failed. Reason: " + e.getMessage(), e);
    }

    int status = method.getStatusCode();
    String statusText = method.getStatusText();

    if (status != 200) {
        throw new RESTLightClientException("GET request failed; HTTP status: " + status + ": " + statusText);
    }

    try {
        return new SAXBuilder().build(method.getResponseBodyAsStream());
    } catch (JDOMException e) {
        throw new RESTLightClientException("Failed to parse response body as XML for GET request.", e);
    } catch (IOException e) {
        throw new RESTLightClientException("Could not retrieve body as a String from GET request.", e);
    } finally {
        method.releaseConnection();
    }
}

From source file:org.sonatype.nexus.restlight.common.AbstractRESTLightClient.java

/**
 * <p>//from  w ww  .  ja v  a 2s .com
 * Low-level DELETE implementation.
 * </p>
 * <p>
 * Submit a DELETE request to the URL given (absolute or relative-to-base-URL depends on urlIsAbsolute flag), and
 * parse the response as an XML {@link Document} (JDOM) instance <b>if</b> expectResponseBody == true. Use the given
 * requestParams map to inject into the HTTP DELETE method.
 * </p>
 */
protected Document doDelete(final String url, final Map<String, ? extends Object> requestParams,
        final boolean urlIsAbsolute, final boolean expectResponseBody) throws RESTLightClientException {
    DeleteMethod method = urlIsAbsolute ? new DeleteMethod(url) : new DeleteMethod(baseUrl + url);

    addRequestParams(method, requestParams);

    try {
        client.executeMethod(method);
    } catch (HttpException e) {
        throw new RESTLightClientException("DELETE request execution failed. Reason: " + e.getMessage(), e);
    } catch (IOException e) {
        throw new RESTLightClientException("DELETE request execution failed. Reason: " + e.getMessage(), e);
    }

    int status = method.getStatusCode();

    String statusText = method.getStatusText();

    if (status != 200) {
        throw new RESTLightClientException("DELETE request failed; HTTP status: " + status + ": " + statusText);
    }

    if (expectResponseBody) {
        try {
            return new SAXBuilder().build(method.getResponseBodyAsStream());
        } catch (JDOMException e) {
            throw new RESTLightClientException("Failed to parse response body as XML for DELETE request.", e);
        } catch (IOException e) {
            throw new RESTLightClientException("Could not retrieve body as a String from DELETE request.", e);
        } finally {
            method.releaseConnection();
        }
    } else {
        method.releaseConnection();

        return null;
    }
}

From source file:org.springbyexample.httpclient.HttpClientOxmTemplate.java

/**
 * Processes <code>HttpMethod</code> by executing the method, 
 * validating the response, and calling the callback.
 * //  w w w.  j av a  2  s.co m
 * @param   httpMethod      <code>HttpMethod</code> to process.
 * @param   callback        Callback with HTTP method's response.
 */
@SuppressWarnings("unchecked")
protected void processHttpMethod(HttpMethod httpMethod, ResponseCallback callback) {
    try {
        client.executeMethod(httpMethod);

        validateResponse(httpMethod);

        if (callback != null) {
            Object value = unmarshaller.unmarshal(new StreamSource(httpMethod.getResponseBodyAsStream()));

            callback.doWithResponse((T) value);
        }
    } catch (HttpException e) {
        throw new HttpAccessException(e.getMessage(), e, httpMethod.getStatusCode());
    } catch (IOException e) {
        throw new HttpAccessException(e.getMessage(), e);
    } finally {
        httpMethod.releaseConnection();
    }
}

From source file:org.springbyexample.httpclient.HttpClientTemplate.java

/**
 * Processes <code>HttpMethod</code> by executing the method, 
 * validating the response, and calling the callback.
 * //from   ww w.j a  v  a 2 s .c om
 * @param   httpMethod      <code>HttpMethod</code> to process.
 * @param   callback        Callback with HTTP method's response.
 */
protected void processHttpMethod(HttpMethod httpMethod, ResponseCallback<?> callback) {
    try {
        client.executeMethod(httpMethod);

        validateResponse(httpMethod);

        if (callback instanceof ResponseByteCallback) {
            ((ResponseByteCallback) callback).doWithResponse(httpMethod.getResponseBody());
        } else if (callback instanceof ResponseStreamCallback) {
            ((ResponseStreamCallback) callback).doWithResponse(httpMethod.getResponseBodyAsStream());
        } else if (callback instanceof ResponseStringCallback) {
            ((ResponseStringCallback) callback).doWithResponse(httpMethod.getResponseBodyAsString());
        }
    } catch (HttpException e) {
        throw new HttpAccessException(e.getMessage(), e, httpMethod.getStatusCode());
    } catch (IOException e) {
        throw new HttpAccessException(e.getMessage(), e);
    } finally {
        httpMethod.releaseConnection();
    }
}

From source file:org.springside.fi.common.httpclient.HttpClientTemplate.java

/**
 * Processes <code>HttpMethod</code> by executing the method, 
 * validating the response, and calling the callback.
 * /*from w  w  w . j  a va  2s.c  o m*/
 * @param   httpMethod      <code>HttpMethod</code> to process.
 * @param   callback        Callback with HTTP method's response.
 */
protected byte[] processHttpMethod(HttpMethod httpMethod) {
    try {
        client.getHttpConnectionManager().getParams().setConnectionTimeout(60000);
        client.getHttpConnectionManager().getParams().setSoTimeout(60000);
        client.executeMethod(httpMethod);

        validateResponse(httpMethod);

        return httpMethod.getResponseBody();
    } catch (HttpException e) {
        throw new HttpAccessException(e.getMessage(), e, httpMethod.getStatusCode());
    } catch (IOException e) {
        throw new HttpAccessException(e.getMessage(), e);
    } finally {
        httpMethod.releaseConnection();
    }
}

From source file:org.tgta.tagger.AnnotationClient.java

public String request(HttpMethod method) throws AnnotationException {

    String response = null;/*from www . j  a  v  a  2s.c o m*/

    // Provide custom retry handler is necessary
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));

    try {
        // Execute the method.
        int statusCode = client.executeMethod(method);

        if (statusCode != HttpStatus.SC_OK) {
            LOG.error("Method failed: " + method.getStatusLine());
        }

        // Read the response body.
        //byte[] responseBody;
        InputStream in = method.getResponseBodyAsStream();

        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        StringBuilder out = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            out.append(line);
        }
        //System.out.println(out.toString());   //Prints the string content read from input stream
        reader.close();

        response = out.toString();

        //TODO Going to buffer response body of large or unknown size. 
        //Using getResponseBodyAsStream instead is recommended.

        // Deal with the response.
        // Use caution: ensure correct character encoding and is not binary data
        //response = new String(responseBody);

    } catch (HttpException e) {
        LOG.error("Fatal protocol violation: " + e.getMessage());
        throw new AnnotationException("Protocol error executing HTTP request.", e);
    } catch (IOException e) {
        LOG.error("Fatal transport error: " + e.getMessage());
        LOG.error(method.getQueryString());
        throw new AnnotationException("Transport error executing HTTP request.", e);
    } finally {
        // Release the connection.
        method.releaseConnection();
    }
    return response;

}

From source file:org.trec.liveqa.GetYAnswersPropertiesFromQid.java

/**
 * //  w ww . j a v a 2  s.co m
 * @param iQid question ID
 * @return map of features and attributes: question title, body, category, best answer, date
 * @throws Exception
 */
public static Map<String, String> extractData(String iQid) throws Exception {

    Map<String, String> res = new LinkedHashMap<>();
    res.put("qid", iQid);

    // parse date from qid
    res.put("Date", DATE_FORMAT.parse(iQid.substring(0, 14)).toString());

    // get and mine html page
    String url = URL_PREFIX + iQid;
    HttpClient client = new HttpClient();
    GetMethod method = new GetMethod(url);
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));
    try {
        int statusCode = client.executeMethod(method);
        if (statusCode != HttpStatus.SC_OK) {
            System.err.println("Method failed: " + method.getStatusLine());
        }
        InputStream responseBody = method.getResponseBodyAsStream();

        // strip top levels
        Document doc = Jsoup.parse(responseBody, "UTF8", url);
        Element html = doc.child(0);

        Element body = html.child(1);
        Element head = html.child(0);

        // get category
        res.put("Top level Category", findElementText(body, cc));

        // get title
        res.put("Title", findElementText(head, ct));

        // get body
        res.put("Body", findElementText(head, cb));

        // get keywords
        res.put("Keywords", findElementText(head, ck));

        // get best answer
        Element best_answer_div = html.select("div#ya-best-answer").first();
        if (best_answer_div != null) {
            res.put("Best Answer", findElementText(best_answer_div, cba));
        }

        responseBody.close();

    } catch (HttpException e) {
        System.err.println("Fatal protocol violation: " + e.getMessage());
        e.printStackTrace();
    } catch (IOException e) {
        System.err.println("Fatal transport error: " + e.getMessage());
        e.printStackTrace();
    } finally {
        method.releaseConnection();
    }

    return res;
}