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

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

Introduction

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

Prototype

public PutMethod(String paramString) 

Source Link

Usage

From source file:muki.tool.JavaCompilationDeploymentTestCase.java

/**
 * This test does nothing as we have to put form params!!
 *///from   w  ww. j av  a 2  s . c o  m
@Test
public void testPutOperation9Json() throws Exception {
    String url = URL_RESOURCE1 + "/pathPutOperation9Json";
    PutMethod method = new PutMethod(url);
}

From source file:muki.tool.JavaCompilationDeploymentTestCase.java

@Test
public void testPutOperation10Xml() throws Exception {
    String url = URL_RESOURCE1 + "/pathPutOperation10Xml";
    PutMethod method = new PutMethod(url);
    String param = this.getXmlCd();
    method.setRequestEntity(new StringRequestEntity(param, "application/xml", null));

    int statusCode = this.getHttpClient().executeMethod(method);
    assertTrue("Method failed: " + method.getStatusLine(), statusCode == HttpStatus.SC_OK);
    byte[] responseBody = method.getResponseBody();
    String jsonResponse = new String(responseBody);
    assertTrue(jsonResponse.indexOf("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>") > -1);
    assertTrue(jsonResponse.indexOf("<title>My Track 1</title>") > -1);
}

From source file:de.mpg.escidoc.pubman.easySubmission.EasySubmission.java

/**
 * Uploads a file to the staging servlet and returns the corresponding URL.
 * /*from   w  w w .ja  va2s. c  o  m*/
 * @param InputStream 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(InputStream in, String mimetype, String userHandle) throws Exception {
    // Prepare the HttpMethod.
    String fwUrl = de.mpg.escidoc.services.framework.ServiceLocator.getFrameworkUrl();
    PutMethod method = new PutMethod(fwUrl + "/st/staging-file");
    method.setRequestEntity(new InputStreamRequestEntity(in));
    method.setRequestHeader("Content-Type", mimetype);
    method.setRequestHeader("Cookie", "escidocCookie=" + userHandle);
    // Execute the method with HttpClient.
    HttpClient client = new HttpClient();
    client.executeMethod(method);
    String response = method.getResponseBodyAsString();
    return xmlTransforming.transformUploadResponseToFileURL(response);
}

From source file:de.mpg.mpdl.inge.xmltransforming.TestBase.java

/**
 * Uploads a file to the staging servlet and returns the corresponding URL.
 * /*from  w w  w .j a v  a 2  s. com*/
 * @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 = PropertyReader.getFrameworkUrl();
    PutMethod method = new PutMethod(fwUrl + "/st/staging-file");

    method.setRequestEntity(new InputStreamRequestEntity(new FileInputStream(filename)));
    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(XmlTransforming.SERVICE_NAME))
            .transformUploadResponseToFileURL(response);
}

From source file:muki.tool.JavaCompilationDeploymentTestCase.java

@Test
public void testPutOperation10Json() throws Exception {
    String url = URL_RESOURCE1 + "/pathPutOperation10Json";
    PutMethod method = new PutMethod(url);
    String param = this.getJsonCd();
    method.setRequestEntity(new StringRequestEntity(param, "application/json", null));

    int statusCode = this.getHttpClient().executeMethod(method);
    assertTrue("Method failed: " + method.getStatusLine(), statusCode == HttpStatus.SC_OK);
    byte[] responseBody = method.getResponseBody();
    String jsonResponse = new String(responseBody);
    assertTrue(jsonResponse.indexOf("\"title\":\"My Track 1\"") > -1);
}

From source file:muki.tool.JavaCompilationDeploymentTestCase.java

@Test
public void testPutOperation11Xml() throws Exception {
    String url = URL_RESOURCE1 + "/pathPutOperation11Xml/41?page=51";
    PutMethod method = new PutMethod(url);
    String param = this.getXmlCd();
    method.setRequestEntity(new StringRequestEntity(param, "application/xml", null));

    int statusCode = this.getHttpClient().executeMethod(method);
    assertTrue("Method failed: " + method.getStatusLine(), statusCode == HttpStatus.SC_OK);
    byte[] responseBody = method.getResponseBody();
    String xmlResponse = new String(responseBody);
    assertTrue(xmlResponse.indexOf("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>") > -1);
    assertTrue(xmlResponse.indexOf("<title>putOperation11-41-51</title>") > -1);
}

From source file:de.mpg.mpdl.inge.xmltransforming.TestBase.java

/**
 * Creates an item with a file in the framework.
 * /*from  ww  w  . j  a  va2  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(PropertyReader.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", PropertyReader.getFrameworkUrl() + href);
    logger.debug("Item=" + item);
    item = ServiceLocator.getItemHandler(userHandle).create(item);
    assertNotNull(item);
    logger.debug("Item=" + item);

    return item;
}

From source file:muki.tool.JavaCompilationDeploymentTestCase.java

@Test
public void testPutOperation11Json() throws Exception {
    String url = URL_RESOURCE1 + "/pathPutOperation11Json/41?page=51";
    PutMethod method = new PutMethod(url);
    String param = this.getJsonCd();
    method.setRequestEntity(new StringRequestEntity(param, "application/json", null));

    int statusCode = this.getHttpClient().executeMethod(method);
    assertTrue("Method failed: " + method.getStatusLine(), statusCode == HttpStatus.SC_OK);
    byte[] responseBody = method.getResponseBody();
    String jsonResponse = new String(responseBody);
    assertTrue(jsonResponse.indexOf("\"title\":\"putOperation11-41-51\"") > -1);
}

From source file:fedora.test.api.TestRESTAPI.java

private HttpResponse putOrPost(String method, Object requestContent, boolean authenticate) throws Exception {
    if (url == null || url.length() == 0) {
        throw new IllegalArgumentException("url must be a non-empty value");
    } else if (!(url.startsWith("http://") || url.startsWith("https://"))) {
        url = getBaseURL() + url;/*from  w  ww.j  ava  2s. c  o m*/
    }

    EntityEnclosingMethod httpMethod = null;
    try {
        if (method.equals("PUT")) {
            httpMethod = new PutMethod(url);
        } else if (method.equals("POST")) {
            httpMethod = new PostMethod(url);
        } else {
            throw new IllegalArgumentException("method must be one of PUT or POST.");
        }

        httpMethod.setDoAuthentication(authenticate);
        httpMethod.getParams().setParameter("Connection", "Keep-Alive");
        if (requestContent != null) {
            httpMethod.setContentChunked(chunked);
            if (requestContent instanceof String) {
                httpMethod.setRequestEntity(
                        new StringRequestEntity((String) requestContent, "text/xml", "utf-8"));
            } else if (requestContent instanceof File) {
                Part[] parts = { new StringPart("param_name", "value"),
                        new FilePart(((File) requestContent).getName(), (File) requestContent) };
                httpMethod.setRequestEntity(new MultipartRequestEntity(parts, httpMethod.getParams()));
            } else {
                throw new IllegalArgumentException("requestContent must be a String or File");
            }
        }
        getClient(authenticate).executeMethod(httpMethod);
        return new HttpResponse(httpMethod);
    } finally {
        if (httpMethod != null) {
            httpMethod.releaseConnection();
        }
    }
}

From source file:muki.tool.JavaCompilationDeploymentTestCase.java

@Test
public void testPutOperation12Json() throws Exception {
    String url = URL_RESOURCE1 + "/pathPutOperation12Json/artists/madonna/tracks/4";
    PutMethod method = new PutMethod(url);
    String param = this.getJsonCd();
    method.setRequestEntity(new StringRequestEntity(param, "application/json", null));

    int statusCode = this.getHttpClient().executeMethod(method);
    assertTrue("Method failed: " + method.getStatusLine(), statusCode == HttpStatus.SC_OK);
    byte[] responseBody = method.getResponseBody();
    String jsonResponse = new String(responseBody);
    assertTrue(jsonResponse.indexOf("\"title\":\"madonna-4\"") > -1);
}