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:com.example.listsync.WebDavRepository.java

private void doUpload(String file, List<String> content) throws IOException {
    LOGGER.info("uploading {}", file);
    LOGGER.info("uploading {} -> {}", file, content);
    PutMethod putMethod = new PutMethod(config.getBaseUrl() + config.getWatchpath() + file);
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    IOUtils.writeLines(content, IOUtils.LINE_SEPARATOR_UNIX, output);
    putMethod.setRequestEntity(new ByteArrayRequestEntity(output.toByteArray()));
    int code = client.executeMethod(putMethod);
    LOGGER.info("upload resultcode: {}", code);
}

From source file:com.eucalyptus.blockstorage.CreateSnapshotTest.java

@Test
public void testSendDummy() throws Exception {
    HttpClient httpClient = new HttpClient();
    String addr = System.getProperty("euca.objectstorage.url") + "/meh/ttt.wsl?gg=vol&hh=snap";

    HttpMethodBase method = new PutMethod(addr);
    method.setRequestHeader("Authorization", "Euca");
    method.setRequestHeader("Date", (new Date()).toString());
    method.setRequestHeader("Expect", "100-continue");

    httpClient.executeMethod(method);/* ww  w  .  j a va 2  s  .  c o  m*/
    String responseString = method.getResponseBodyAsString();
    System.out.println(responseString);
    method.releaseConnection();
}

From source file:com.eucalyptus.blockstorage.tests.CreateSnapshotTest.java

@Test
public void testSendDummy() throws Exception {
    HttpClient httpClient = new HttpClient();
    String addr = System.getProperty(WalrusProperties.URL_PROPERTY) + "/meh/ttt.wsl?gg=vol&hh=snap";

    HttpMethodBase method = new PutMethod(addr);
    method.setRequestHeader("Authorization", "Euca");
    method.setRequestHeader("Date", (new Date()).toString());
    method.setRequestHeader("Expect", "100-continue");

    httpClient.executeMethod(method);//from   w ww.ja v  a 2  s  .  c o m
    String responseString = method.getResponseBodyAsString();
    System.out.println(responseString);
    method.releaseConnection();
}

From source file:edu.htwm.vsp.phoebook.rest.client.RestClient.java

public PhoneUser verifyAddNumber(String userID, String phoneCaption, String phoneNumber, int expectedStatusCode,
        String contentType) throws IOException, JAXBException, Exception {

    System.out.println("Add PhoneNumber: caption: " + phoneCaption + ", number: " + phoneNumber
            + " for PhoneUser: " + userID + " ... \n");

    HttpClient client = new HttpClient();
    // -- build HTTP PUT request
    // escapt phoneNumber, da QueryParam
    PutMethod method = new PutMethod(getServiceBaseURI() + "/users" + "/" + userID + "/numbers/" + phoneCaption
            + "?number=" + escapeHtml(phoneNumber));

    // -- determine the mime type you wish to receive here 
    method.setRequestHeader("Accept", contentType);

    int responseCode = client.executeMethod(method);

    assertEquals(expectedStatusCode, responseCode);

    String response = responseToString(method.getResponseBodyAsStream());
    System.out.println(response);
    String content = method.getResponseHeader("Content-Type").getValue();
    /* Antwort zurckgeben */
    return getUserFromResponse(response, content);

}

From source file:eu.learnpad.core.impl.or.XwikiCoreFacadeRestResource.java

@Override
public void pushKPIValues(String modelSetId, KPIValuesFormat format, String businessActorId,
        InputStream cockpitContent) throws LpRestException {
    String contentType = "application/xml";

    HttpClient httpClient = this.getAnonymousClient();
    String uri = String.format("%s/learnpad/or/corefacade/pushkpivalues/%s", DefaultRestResource.REST_URI,
            modelSetId);/*from w  w  w  . j  av a  2  s . co  m*/
    PutMethod putMethod = new PutMethod(uri);
    putMethod.addRequestHeader("Content-Type", contentType);

    NameValuePair[] queryString = new NameValuePair[2];
    queryString[0] = new NameValuePair("format", format.toString());
    queryString[1] = new NameValuePair("businessactor", businessActorId);
    putMethod.setQueryString(queryString);

    RequestEntity requestEntity = new InputStreamRequestEntity(cockpitContent);
    putMethod.setRequestEntity(requestEntity);

    try {
        httpClient.executeMethod(putMethod);
    } catch (IOException e) {
        throw new LpRestExceptionXWikiImpl(e.getMessage(), e);
    }
}

From source file:com.sixdimensions.wcm.cq.dao.webdav.WebDavDAO.java

/**
 * Uploads the file into CQ.//from  w  w w .  j  a  v a2s. co m
 * 
 * @param file
 *            The file to upload
 * @param path
 *            the path to which to upload the file
 * @throws IOException
 */
public void uploadFile(final File file, final String path) throws IOException {
    this.log.debug("uploadFile");
    final String url = this.config.getHost() + ":" + this.config.getPort() + path + "/" + file.getName();
    final PutMethod put = new PutMethod(url);

    put.setRequestEntity(new InputStreamRequestEntity(new FileInputStream(file)));
    final int status = this.httpClient.executeMethod(put);
    if ((status != 200) && (status != 201) && (status != 204)) {
        throw new IOException("uploadFile(" + url + ") failed, status code=" + status);
    }
}

From source file:com.sun.syndication.propono.atom.client.ClientEntry.java

/**
 * Update entry by posting new representation of entry to server.
 * Note that you should not attempt to update entries that you get from
 * iterating over a collection they may be "partial" entries. If you want 
 * to update an entry, you must get it via one of the <code>getEntry()</code>
 * methods in //from w ww  .  j a v a 2 s. co m
 * {@link com.sun.syndication.propono.atom.common.Collection} or 
 * {@link com.sun.syndication.propono.atom.common.AtomService}.
 * @throws ProponoException If entry is a "partial" entry.
 */
public void update() throws ProponoException {
    if (partial)
        throw new ProponoException("ERROR: attempt to update partial entry");
    EntityEnclosingMethod method = new PutMethod(getEditURI());
    addAuthentication(method);
    StringWriter sw = new StringWriter();
    int code = -1;
    try {
        Atom10Generator.serializeEntry(this, sw);
        method.setRequestEntity(new StringRequestEntity(sw.toString()));
        method.setRequestHeader("Content-type", "application/atom+xml; charset=utf-8");
        getHttpClient().executeMethod(method);
        InputStream is = method.getResponseBodyAsStream();
        if (method.getStatusCode() != 200 && method.getStatusCode() != 201) {
            throw new ProponoException(
                    "ERROR HTTP status=" + method.getStatusCode() + " : " + Utilities.streamToString(is));
        }

    } catch (Exception e) {
        String msg = "ERROR: updating entry, HTTP code: " + code;
        logger.debug(msg, e);
        throw new ProponoException(msg, e);
    } finally {
        method.releaseConnection();
    }
}

From source file:ch.cyberduck.core.dav.DAVResource.java

/**
 * Execute the PUT method for the given path.
 *
 * @param path          the server relative path to put the data
 * @param requestEntity The input stream.
 * @return true if the method is succeeded.
 * @throws IOException/*w ww .j  a  va 2s . co m*/
 */
public boolean putMethod(String path, RequestEntity requestEntity) throws IOException {

    setClient();

    PutMethod method = new PutMethod(URIUtil.encodePathQuery(path)) {
        @Override
        public boolean getFollowRedirects() {
            // See #3206. Redirects for uploads are not allowed without user interaction by default.
            return true;
        }
    };

    // Activates 'Expect: 100-Continue' handshake. The purpose of
    // the 'Expect: 100-Continue' handshake to allow a client that is
    // sending a request message with a request body to determine if
    // the origin server is willing to accept the request (based on
    // the request headers) before the client sends the request body.
    //
    // Otherwise, upload will fail when using digest authentication.
    // Fix #2268
    method.setUseExpectHeader(true);

    generateIfHeader(method);
    method.setRequestEntity(requestEntity);

    generateTransactionHeader(method);
    generateAdditionalHeaders(method);
    int statusCode = client.executeMethod(method);

    method.releaseConnection();

    setStatusCode(statusCode, method.getStatusText());
    return isHttpSuccess(statusCode);
}

From source file:com.dtolabs.client.utils.HttpClientChannel.java

/**
 * Create new HttpMethod objects for the requestUrl, and set any request headers specified previously
 *//*w w w  .  j  a  v  a2s  .  c o m*/
private HttpMethod initMethod() {
    if ("GET".equalsIgnoreCase(getMethodType())) {
        httpMethod = new GetMethod(requestUrl);
    } else if ("POST".equalsIgnoreCase(getMethodType())) {
        httpMethod = new PostMethod(requestUrl);
    } else if ("PUT".equalsIgnoreCase(getMethodType())) {
        httpMethod = new PutMethod(requestUrl);
    } else if ("DELETE".equalsIgnoreCase(getMethodType())) {
        httpMethod = new DeleteMethod(requestUrl);
    } else {
        throw new IllegalArgumentException("Unknown method type: " + getMethodType());
    }
    if (reqHeaders.size() > 0) {
        for (Iterator i = reqHeaders.keySet().iterator(); i.hasNext();) {
            String s = (String) i.next();
            String v = (String) reqHeaders.get(s);
            httpMethod.setRequestHeader(s, v);
        }
    }
    return httpMethod;
}

From source file:com.rometools.propono.atom.client.ClientEntry.java

/**
 * Update entry by posting new representation of entry to server. Note that you should not
 * attempt to update entries that you get from iterating over a collection they may be "partial"
 * entries. If you want to update an entry, you must get it via one of the
 * <code>getEntry()</code> methods in {@link com.rometools.rome.propono.atom.common.Collection}
 * or {@link com.rometools.rome.propono.atom.common.AtomService}.
 *
 * @throws ProponoException If entry is a "partial" entry.
 *//*from   w  w  w  .  ja  v a2 s.  co m*/
public void update() throws ProponoException {
    if (partial) {
        throw new ProponoException("ERROR: attempt to update partial entry");
    }
    final EntityEnclosingMethod method = new PutMethod(getEditURI());
    addAuthentication(method);
    final StringWriter sw = new StringWriter();
    final int code = -1;
    try {
        Atom10Generator.serializeEntry(this, sw);
        method.setRequestEntity(new StringRequestEntity(sw.toString(), null, null));
        method.setRequestHeader("Content-type", "application/atom+xml; charset=utf-8");
        getHttpClient().executeMethod(method);
        final InputStream is = method.getResponseBodyAsStream();
        if (method.getStatusCode() != 200 && method.getStatusCode() != 201) {
            throw new ProponoException(
                    "ERROR HTTP status=" + method.getStatusCode() + " : " + Utilities.streamToString(is));
        }

    } catch (final Exception e) {
        final String msg = "ERROR: updating entry, HTTP code: " + code;
        LOG.debug(msg, e);
        throw new ProponoException(msg, e);
    } finally {
        method.releaseConnection();
    }
}