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.vast.jpip.JPIPHttpClient.java

public void sendRequest() throws IOException {
    newRequest = true;//  w  w  w  .  jav a 2  s  .  c o m

    // encode request as HTTP GET
    encoder.setJpipRequestFields(request);
    String url = serverUrl + encoder.createGetRequest();
    URL requestUrl = new URL(url);
    System.out.println("Sending GET request: " + url);

    // open connection and set HTTP options      
    method = new GetMethod(url);

    try {
        method.addRequestHeader("Accept", "image/" + request.serverControlField.type[0]);
        method.addRequestHeader("Host", requestUrl.getHost());
        method.addRequestHeader("Connection", "keep-alive");

        int statusCode = httpClient.executeMethod(method);
        if (statusCode != HttpStatus.SC_OK)
            System.err.println("Method failed: " + method.getStatusLine());
    } 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();
    }
}

From source file:org.wingsource.plugin.impl.gadget.bean.Gadget.java

private Response getResponse(String tokenId, String href, Map<String, String> requestParameters) {
    logger.finest("Fetching content using HttpClient....user-Id: " + tokenId);
    HttpClient hc = new HttpClient();
    hc.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
    HttpMethod method = new GetMethod(href);
    method.addRequestHeader("xx-wings-user-id", tokenId);
    ArrayList<NameValuePair> nvpList = new ArrayList<NameValuePair>();
    Set<String> keys = requestParameters.keySet();
    for (String key : keys) {
        String value = requestParameters.get(key);
        nvpList.add(new NameValuePair(key, value));
    }//from  w w w . j a va  2s. co m

    String qs = method.getQueryString();
    if (qs != null) {
        String[] nvPairs = qs.split("&");
        for (String nvPair : nvPairs) {
            String[] mapping = nvPair.split("=");
            nvpList.add(new NameValuePair(mapping[0], mapping[1]));
        }
    }
    method.setFollowRedirects(true);

    NameValuePair[] nvps = new NameValuePair[nvpList.size()];
    nvps = nvpList.toArray(nvps);
    method.setQueryString(nvps);

    byte[] content = null;
    Header[] headers = null;
    try {
        hc.executeMethod(method);
        content = method.getResponseBody();
        headers = method.getResponseHeaders();
    } catch (HttpException e) {
        logger.log(Level.SEVERE, e.getMessage(), e);
    } catch (IOException e) {
        logger.log(Level.SEVERE, e.getMessage(), e);
    }
    return new Response(content, headers);
}

From source file:org.wingsource.plugin.impl.gadget.bean.Gadget.java

private InputStream getContentStream(String href) {
    logger.finest("Fetching content using HttpClient....");
    HttpClient hc = new HttpClient();
    hc.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
    HttpMethod method = new GetMethod(href);
    method.setFollowRedirects(true);/*from  w w  w.j  a  v  a2  s  .c  o m*/

    InputStream responseStream = null;
    try {
        hc.executeMethod(method);
        responseStream = method.getResponseBodyAsStream();
    } catch (HttpException e) {
        logger.log(Level.SEVERE, e.getMessage(), e);
    } catch (IOException e) {
        logger.log(Level.SEVERE, e.getMessage(), e);
    }
    return responseStream;
}

From source file:org.wise.portal.domain.admin.DailyAdminJob.java

/**
 * POSTs WISE usage statistics to central hub
 */// w w w .  ja  va  2s  .  c o m
public void postStatistics(String wiseStatisticsString) {

    if (WISE_HUB_URL != null) {
        HttpClient client = new HttpClient();

        // Create a method instance.
        PostMethod method = new PostMethod(WISE_HUB_URL);

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

        method.addParameter("name", wiseProperties.getProperty("wise.name"));
        method.addParameter("wiseBaseURL", wiseProperties.getProperty("wiseBaseURL"));
        method.addParameter("stats", wiseStatisticsString);

        try {

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

            if (statusCode != HttpStatus.SC_OK) {
                System.err.println("Method failed: " + method.getStatusLine());
            }
            // Read the response body.
            //byte[] responseBody = null;
            //responseBody = method.getResponseBody();
            //String responseString = new String(responseBody);
            //System.out.println(responseString);

            // Deal with the response.
            // Use caution: ensure correct character encoding and is not binary data
        } 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 {
            // Release the connection.
            method.releaseConnection();
        }
    }
}

From source file:org.xwiki.test.webdav.AbstractWebDAVTest.java

/**
 * Executes the given {@link HttpMethod} and tests for the expected return status.
 * //from   w ww .  j  av a  2 s.co  m
 * @param method the {@link HttpMethod}.
 * @param expect expected return status.
 */
protected void testMethod(HttpMethod method, int expect) {
    try {
        int status = getHttpClient().executeMethod(method);
        assertEquals(expect, status);
    } catch (HttpException ex) {
        fail(ex.getMessage());
    } catch (IOException ex) {
        fail(ex.getMessage());
    }
}

From source file:org.xwiki.test.webdav.DefaultWebDAVTest.java

/**
 * Test create and delete space.//w ww.  j  ava2 s  . c om
 */
public void testCreateAndDeleteSpace() {
    String spaceUrl = SPACES + "/TestSpace";
    DeleteMethod deleteMethod = new DeleteMethod();
    deleteMethod.setDoAuthentication(true);
    MkcolMethod mkColMethod = new MkcolMethod();
    mkColMethod.setDoAuthentication(true);
    try {
        deleteMethod.setPath(spaceUrl);
        assertEquals(DavServletResponse.SC_NO_CONTENT, getHttpClient().executeMethod(deleteMethod));
        mkColMethod.setPath(spaceUrl);
        assertEquals(DavServletResponse.SC_CREATED, getHttpClient().executeMethod(mkColMethod));
        deleteMethod.setPath(spaceUrl);
        assertEquals(DavServletResponse.SC_NO_CONTENT, getHttpClient().executeMethod(deleteMethod));
    } catch (HttpException ex) {
        fail(ex.getMessage());
    } catch (IOException ex) {
        fail(ex.getMessage());
    }
}

From source file:org.xwiki.test.webdav.DefaultWebDAVTest.java

/**
 * Test rename space.//from   w w  w  .j a v a  2  s.  c om
 */
public void testRenameSpace() {
    String spaceUrl = SPACES + "/TestSpace";
    String relativeDestinationPath = "/xwiki/webdav/spaces/RenamedTestSpace";
    String movedSpaceUrl = SPACES + "/RenamedTestSpace";
    DeleteMethod deleteMethod = new DeleteMethod();
    deleteMethod.setDoAuthentication(true);
    MkcolMethod mkColMethod = new MkcolMethod();
    mkColMethod.setDoAuthentication(true);
    MoveMethod moveMethod = new MoveMethod();
    moveMethod.setDoAuthentication(true);
    try {
        deleteMethod.setPath(spaceUrl);
        assertEquals(DavServletResponse.SC_NO_CONTENT, getHttpClient().executeMethod(deleteMethod));
        deleteMethod.setPath(movedSpaceUrl);
        assertEquals(DavServletResponse.SC_NO_CONTENT, getHttpClient().executeMethod(deleteMethod));
        mkColMethod.setPath(spaceUrl);
        assertEquals(DavServletResponse.SC_CREATED, getHttpClient().executeMethod(mkColMethod));
        moveMethod.setPath(spaceUrl);
        moveMethod.setDestination(relativeDestinationPath);
        assertEquals(DavServletResponse.SC_CREATED, getHttpClient().executeMethod(moveMethod));
        deleteMethod.setPath(movedSpaceUrl);
        assertEquals(DavServletResponse.SC_NO_CONTENT, getHttpClient().executeMethod(deleteMethod));
    } catch (HttpException ex) {
        fail(ex.getMessage());
    } catch (IOException ex) {
        fail(ex.getMessage());
    }
}

From source file:org.xwiki.test.webdav.DefaultWebDAVTest.java

/**
 * Test create and delete page./* ww w.jav a2s  .co m*/
 */
public void testCreateAndDeletePage() {
    String spaceUrl = SPACES + "/TestSpace";
    String pageUrl = spaceUrl + "/TestPage";
    DeleteMethod deleteMethod = new DeleteMethod();
    deleteMethod.setDoAuthentication(true);
    MkcolMethod mkColMethod = new MkcolMethod();
    mkColMethod.setDoAuthentication(true);
    try {
        deleteMethod.setPath(spaceUrl);
        assertEquals(DavServletResponse.SC_NO_CONTENT, getHttpClient().executeMethod(deleteMethod));
        mkColMethod.setPath(spaceUrl);
        assertEquals(DavServletResponse.SC_CREATED, getHttpClient().executeMethod(mkColMethod));
        mkColMethod.setPath(pageUrl);
        assertEquals(DavServletResponse.SC_CREATED, getHttpClient().executeMethod(mkColMethod));
        deleteMethod.setPath(pageUrl);
        assertEquals(DavServletResponse.SC_NO_CONTENT, getHttpClient().executeMethod(deleteMethod));
        deleteMethod.setPath(spaceUrl);
        assertEquals(DavServletResponse.SC_NO_CONTENT, getHttpClient().executeMethod(deleteMethod));
    } catch (HttpException ex) {
        fail(ex.getMessage());
    } catch (IOException ex) {
        fail(ex.getMessage());
    }
}

From source file:org.xwiki.test.webdav.DefaultWebDAVTest.java

/**
 * Test get page content./* w  w w .  ja  v a 2  s.co  m*/
 */
public void testGetPageWikiContent() {
    String spaceUrl = SPACES + "/TestSpace";
    String pageUrl = spaceUrl + "/TestPage";
    String wikiTextFileUrl = pageUrl + "/wiki.txt";
    String wikiXMLFileUrl = pageUrl + "/wiki.xml";
    DeleteMethod deleteMethod = new DeleteMethod();
    deleteMethod.setDoAuthentication(true);
    MkcolMethod mkColMethod = new MkcolMethod();
    mkColMethod.setDoAuthentication(true);
    GetMethod getMethod = new GetMethod();
    getMethod.setDoAuthentication(true);
    try {
        deleteMethod.setPath(spaceUrl);
        assertEquals(DavServletResponse.SC_NO_CONTENT, getHttpClient().executeMethod(deleteMethod));
        mkColMethod.setPath(spaceUrl);
        assertEquals(DavServletResponse.SC_CREATED, getHttpClient().executeMethod(mkColMethod));
        mkColMethod.setPath(pageUrl);
        assertEquals(DavServletResponse.SC_CREATED, getHttpClient().executeMethod(mkColMethod));
        getMethod.setPath(wikiTextFileUrl);
        assertEquals(DavServletResponse.SC_OK, getHttpClient().executeMethod(getMethod));
        assertTrue(getMethod.getResponseBodyAsStream().read() != -1);
        getMethod.setPath(wikiXMLFileUrl);
        assertEquals(DavServletResponse.SC_OK, getHttpClient().executeMethod(getMethod));
        assertTrue(getMethod.getResponseBodyAsStream().read() != -1);
        deleteMethod.setPath(pageUrl);
        assertEquals(DavServletResponse.SC_NO_CONTENT, getHttpClient().executeMethod(deleteMethod));
        deleteMethod.setPath(spaceUrl);
        assertEquals(DavServletResponse.SC_NO_CONTENT, getHttpClient().executeMethod(deleteMethod));
    } catch (HttpException ex) {
        fail(ex.getMessage());
    } catch (IOException ex) {
        fail(ex.getMessage());
    }
}

From source file:org.xwiki.test.webdav.DefaultWebDAVTest.java

/**
 * Test update page content./*from   w w  w.  j  av a2 s .co  m*/
 */
public void testUpdatePageWikiContent() {
    String spaceUrl = SPACES + "/TestSpace";
    String pageUrl = spaceUrl + "/TestPage";
    String wikiTextFileUrl = pageUrl + "/wiki.txt";
    String wikiXMLFileUrl = pageUrl + "/wiki.xml";
    String newContent = "New Content";
    DeleteMethod deleteMethod = new DeleteMethod();
    deleteMethod.setDoAuthentication(true);
    MkcolMethod mkColMethod = new MkcolMethod();
    mkColMethod.setDoAuthentication(true);
    PutMethod putMethod = new PutMethod();
    putMethod.setDoAuthentication(true);
    GetMethod getMethod = new GetMethod();
    getMethod.setDoAuthentication(true);
    try {
        deleteMethod.setPath(spaceUrl);
        assertEquals(DavServletResponse.SC_NO_CONTENT, getHttpClient().executeMethod(deleteMethod));
        mkColMethod.setPath(spaceUrl);
        assertEquals(DavServletResponse.SC_CREATED, getHttpClient().executeMethod(mkColMethod));
        mkColMethod.setPath(pageUrl);
        assertEquals(DavServletResponse.SC_CREATED, getHttpClient().executeMethod(mkColMethod));
        putMethod.setPath(wikiTextFileUrl);
        putMethod.setRequestEntity(
                new InputStreamRequestEntity(new ByteArrayInputStream(newContent.getBytes())));
        // Already existing resource, in which case SC_NO_CONTENT will be the return status.
        assertEquals(DavServletResponse.SC_NO_CONTENT, getHttpClient().executeMethod(putMethod));
        getMethod.setPath(wikiTextFileUrl);
        assertEquals(DavServletResponse.SC_OK, getHttpClient().executeMethod(getMethod));
        assertEquals(newContent, getMethod.getResponseBodyAsString());
        putMethod.setPath(wikiXMLFileUrl);
        putMethod.setRequestEntity(
                new InputStreamRequestEntity(new ByteArrayInputStream(newContent.getBytes())));
        // XML saving was disabled recently. See http://jira.xwiki.org/jira/browse/XWIKI-2910
        assertEquals(DavServletResponse.SC_METHOD_NOT_ALLOWED, getHttpClient().executeMethod(putMethod));
        deleteMethod.setPath(pageUrl);
        assertEquals(DavServletResponse.SC_NO_CONTENT, getHttpClient().executeMethod(deleteMethod));
        deleteMethod.setPath(spaceUrl);
        assertEquals(DavServletResponse.SC_NO_CONTENT, getHttpClient().executeMethod(deleteMethod));
    } catch (HttpException ex) {
        fail(ex.getMessage());
    } catch (IOException ex) {
        fail(ex.getMessage());
    }
}