Example usage for org.apache.commons.httpclient.methods InputStreamRequestEntity InputStreamRequestEntity

List of usage examples for org.apache.commons.httpclient.methods InputStreamRequestEntity InputStreamRequestEntity

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.methods InputStreamRequestEntity InputStreamRequestEntity.

Prototype

public InputStreamRequestEntity(InputStream paramInputStream) 

Source Link

Usage

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

/**
 * Tests the PUT method on the given url.
 * //from   w w w.ja v  a 2s.co  m
 * @param url the target url.
 * @param content the content for the {@link PutMethod}.
 * @param expect the return status expected.
 * @return the {@link HttpMethod} which contains the response.
 */
protected HttpMethod put(String url, String content, int expect) throws Exception {
    PutMethod putMethod = new PutMethod();
    putMethod.setDoAuthentication(true);
    putMethod.setPath(url);
    putMethod.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(content.getBytes())));
    testMethod(putMethod, expect);
    return putMethod;
}

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

/**
 * Test update page content./*from  w  w  w .  j  av a 2s  .  com*/
 */
@Test
public void testUpdatePageWikiContent() throws Exception {
    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);

    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 https://jira.xwiki.org/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));
}

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

/**
 * Test making attachment./*w ww  . ja v a  2  s.  c o  m*/
 */
@Test
public void testMakingAttachment() throws Exception {
    String spaceUrl = SPACES + "/TestSpace";
    String pageUrl = spaceUrl + "/TestPage";
    String attachmentUrl = pageUrl + "/attachment.txt";
    String attachmentContent = "Attachment 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);

    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(attachmentUrl);
    assertEquals(DavServletResponse.SC_NOT_FOUND, getHttpClient().executeMethod(getMethod));
    putMethod.setPath(attachmentUrl);
    putMethod.setRequestEntity(
            new InputStreamRequestEntity(new ByteArrayInputStream(attachmentContent.getBytes())));
    assertEquals(DavServletResponse.SC_CREATED, getHttpClient().executeMethod(putMethod));
    getMethod.setPath(attachmentUrl);
    assertEquals(DavServletResponse.SC_OK, getHttpClient().executeMethod(getMethod));
    assertEquals(attachmentContent, getMethod.getResponseBodyAsString());
    deleteMethod.setPath(attachmentUrl);
    assertEquals(DavServletResponse.SC_NO_CONTENT, getHttpClient().executeMethod(deleteMethod));
    deleteMethod.setPath(pageUrl);
    assertEquals(DavServletResponse.SC_NO_CONTENT, getHttpClient().executeMethod(deleteMethod));
    deleteMethod.setPath(spaceUrl);
    assertEquals(DavServletResponse.SC_NO_CONTENT, getHttpClient().executeMethod(deleteMethod));
}

From source file:org.ybygjy.httpclient.ChunkEncodedPost.java

public static void main(String[] args) throws Exception {
    /*/*ww w .jav a 2 s  .  co m*/
    if (args.length != 1)  {
        System.out.println("Usage: ChunkEncodedPost <file>");
        System.out.println("<file> - full path to a file to be posted");
        System.exit(1);
    }
    */
    HttpClient client = new HttpClient();

    PostMethod httppost = new PostMethod("http://localhost:8080/org.ybygjy.web.servlet.HttpClientServlet");

    File file = new File("D:\\DEV\\04_mywork\\001_mywork\\src\\org\\ybygjy\\httpclient\\ChunkEncodedPost.java");

    httppost.setRequestEntity(new InputStreamRequestEntity(new FileInputStream(file)));
    httppost.setContentChunked(true);

    try {
        int rtnFlag = client.executeMethod(httppost);
        System.out.println("rtnFlag==>" + rtnFlag);
        if (httppost.getStatusCode() == HttpStatus.SC_OK) {
            System.out.println(httppost.getResponseBodyAsString());
        } else {
            System.out.println("Unexpected failure: " + httppost.getStatusLine().toString());
        }
        Header[] headers = httppost.getResponseHeaders();
        for (Header header : headers) {
            System.out.println(header.getName() + ":" + header.getValue());
        }
        System.out.println("+");
        System.out.println(new String(httppost.getResponseBody()));
    } finally {
        httppost.releaseConnection();
    }
}

From source file:semantic.diversification.semantic.OpenCalaisRestCaller.java

private PostMethod createPostMethod(InputStream input) {
    PostMethod method = new PostMethod(CALAIS_API_ENDPOINT);
    // Set mandatory parameters
    method.setRequestHeader("x-calais-licenseID", CALAIS_API_KEY);
    // Set input content type
    method.setRequestHeader("Content-Type", "text/xml; charset=UTF-8");
    // Set response/output format
    method.setRequestHeader("Accept", "xml/rdf");
    // Enable Social Tags processing
    method.setRequestHeader("enableMetadataType", "SocialTags");
    // Add the file contents as request entity
    method.setRequestEntity(new InputStreamRequestEntity(input));

    return method;
}

From source file:sos.scheduler.job.JobSchedulerCheckUpdates.java

private int sendRequest(final String contentType, final String url) throws Exception {
    int rc = 0;/*w w  w.j a v  a  2  s  .co m*/

    try {

        spooler_log_debug3("... Send request:" + url);
        PostMethod post = new PostMethod(url);
        post.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(contentType.getBytes())));
        post.setRequestHeader("Content-type", "xml");

        HttpClient httpClient = new HttpClient();
        if (!http_proxy.equals("")) {
            httpClient.getHostConfiguration().setProxy(http_proxy, http_proxy_port);
        }
        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(timeout);
        httpClient.getHttpConnectionManager().getParams().setSoTimeout(timeout);

        // deprecated httpClient.setTimeout(30*1000);
        rc = httpClient.executeMethod(post);
        spooler_log_debug3("... request (flgOperationWasSuccessful):" + rc);

        getResponse(post.getResponseBodyAsStream());
        return rc;

    } catch (Exception e) {
        throw new Exception(
                "could not connect to SOS Web Service, an error occurred in HTTP POST: " + e.getMessage());
    }
}

From source file:test.common.TestBase.java

/**
 * Uploads a file to the staging servlet and returns the corresponding URL.
 * /*from  w  w w .  j  av  a2  s .c om*/
 * @param fileName The file to upload
 * @param mimetype The mimetype of the file
 * @param userHandle The userHandle to use for upload
 * @return The URL of the uploaded file.
 * @throws Exception If anything goes wrong...
 */
protected URL uploadFile(String fileName, String mimetype, final String userHandle) throws Exception {
    // Prepare the HttpMethod.
    String fwUrl = ServiceLocator.getFrameworkUrl();
    PutMethod method = new PutMethod(fwUrl + "/st/staging-file");
    logger.info("Framework: " + fwUrl);
    File file = ResourceUtil.getResourceAsFile(fileName, TestBase.class.getClassLoader());
    method.setRequestEntity(new InputStreamRequestEntity(new FileInputStream(file)));
    method.setRequestHeader("Content-Type", mimetype);
    method.setRequestHeader("Cookie", "escidocCookie=" + userHandle);
    // Execute the method with HttpClient.
    HttpClient client = new HttpClient();
    ProxyHelper.executeMethod(client, method);
    String response = method.getResponseBodyAsString();
    assertEquals(HttpServletResponse.SC_OK, method.getStatusCode());
    return ((XmlTransforming) getService(
            "ejb:common_logic_ear/common_logic/XmlTransformingBean!" + XmlTransforming.class.getName()))
                    .transformUploadResponseToFileURL(response);
}

From source file:test.common.TestBase.java

/**
 * Creates an item with a file in the framework.
 * /*from  w ww . j av a  2 s . c o m*/
 * @param userHandle The userHandle of a user with the appropriate grants.
 * @return The XML of the created item with a file, given back by the framework.
 * @throws Exception Any exception
 */
protected String createItemWithFile(String userHandle) throws Exception {
    // Prepare the HttpMethod.
    PutMethod method = new PutMethod(ServiceLocator.getFrameworkUrl() + "/st/staging-file");
    method.setRequestEntity(new InputStreamRequestEntity(new FileInputStream(COMPONENT_FILE)));
    method.setRequestHeader("Content-Type", MIME_TYPE);
    method.setRequestHeader("Cookie", "escidocCookie=" + userHandle);
    // Execute the method with HttpClient.
    HttpClient client = new HttpClient();
    ProxyHelper.executeMethod(client, method);
    logger.debug("Status=" + method.getStatusCode()); // >= HttpServletResponse.SC_MULTIPLE_CHOICE 300 ???
    assertEquals(HttpServletResponse.SC_OK, method.getStatusCode());
    String response = method.getResponseBodyAsString();
    logger.debug("Response=" + response);
    // Create a document from the response.
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
    Document document = docBuilder.parse(method.getResponseBodyAsStream());
    document.getDocumentElement().normalize();
    // Extract the file information.
    String href = getValue(document, "/staging-file/@href");
    assertNotNull(href);
    // Create an item with the href in the component.
    String item = readFile(ITEM_FILE);
    item = item.replaceFirst("XXX_CONTENT_REF_XXX", ServiceLocator.getFrameworkUrl() + href);
    logger.debug("Item=" + item);
    item = ServiceLocator.getItemHandler(userHandle).create(item);
    assertNotNull(item);
    logger.debug("Item=" + item);
    return item;
}

From source file:test.framework.sb.TestSimpleSearch.java

private String createReleasedItemWithFile() throws Exception {
    String userHandle = loginScientist();
    // Prepare the HttpMethod.
    PutMethod method = new PutMethod(ServiceLocator.getFrameworkUrl() + "/st/staging-file");
    method.setRequestEntity(new InputStreamRequestEntity(
            new FileInputStream("src/test/resourcestest/testsimplesearch/Der_kleine_Prinz_Auszug.pdf")));
    method.setRequestHeader("Content-Type", "application/pdf");
    method.setRequestHeader("Cookie", "escidocCookie=Skip-Authorization");

    // Execute the method with HttpClient.
    HttpClient client = new HttpClient();
    ProxyHelper.executeMethod(client, method);
    logger.debug("Status=" + method.getStatusCode()); // >= HttpServletResponse.SC_MULTIPLE_CHOICE 300 ???
    assertEquals(HttpServletResponse.SC_OK, method.getStatusCode());
    String response = method.getResponseBodyAsString();
    logger.debug("Response=" + response);

    // Create a document from the response.
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
    Document document = docBuilder.parse(method.getResponseBodyAsStream());
    document.getDocumentElement().normalize();

    // Extract the file information.
    String href = getValue(document, "/staging-file/@href");
    assertNotNull(href);/*from  w ww .  ja  v a  2 s . c om*/

    // Create an item with the href in the component.
    String item = readFile("src/test/resources/test/testsimplesearch/item1.xml");
    item = item.replaceFirst("XXX_CONTENT_REF_XXX", ServiceLocator.getFrameworkUrl() + href);
    logger.debug("Item=" + item);
    item = ServiceLocator.getItemHandler(userHandle).create(item);
    assertNotNull(item);
    logger.debug("Item=" + item);

    String id = getId(item);
    String md = getModificationDate(item);
    ServiceLocator.getItemHandler(userHandle).submit(id, createModificationDate(md));
    item = ServiceLocator.getItemHandler(userHandle).retrieve(id);
    md = getModificationDate(item);
    ServiceLocator.getItemHandler(userHandle).release(id, createModificationDate(md));
    return id;
}

From source file:test.framework.st.TestFile.java

private String createItemWithFile(String handle) throws Exception {
    // Prepare the HttpMethod.
    PutMethod method = new PutMethod(ServiceLocator.getFrameworkUrl() + "/st/staging-file");
    method.setRequestEntity(new InputStreamRequestEntity(new FileInputStream(COMPONENT_FILE)));
    method.setRequestHeader("Content-Type", MIME_TYPE);
    method.setRequestHeader("Cookie", "escidocCookie=" + handle);
    // Execute the method with HttpClient.
    HttpClient client = new HttpClient();
    ProxyHelper.executeMethod(client, method);
    logger.debug("Status=" + method.getStatusCode()); // >= HttpServletResponse.SC_MULTIPLE_CHOICE 300 ???
    assertEquals(HttpServletResponse.SC_OK, method.getStatusCode());
    String response = method.getResponseBodyAsString();
    logger.debug("Response=" + response);
    // Create a document from the response.
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
    Document document = docBuilder.parse(method.getResponseBodyAsStream());
    document.getDocumentElement().normalize();
    // Extract the file information.
    String href = getValue(document, "/staging-file/@href");
    assertNotNull(href);//from   w  ww. ja v a  2  s.c om
    // Create an item with the href in the component.
    String item = readFile(ITEM_FILE);
    item = item.replaceFirst("XXX_CONTENT_REF_XXX", ServiceLocator.getFrameworkUrl() + href);
    logger.debug("Item (before create)=" + item);
    item = ServiceLocator.getItemHandler(handle).create(item);
    assertNotNull(item);
    logger.debug("Item (after create)=" + item);
    return item;
}