Example usage for org.apache.http.client.methods HttpPut HttpPut

List of usage examples for org.apache.http.client.methods HttpPut HttpPut

Introduction

In this page you can find the example usage for org.apache.http.client.methods HttpPut HttpPut.

Prototype

public HttpPut(final String uri) 

Source Link

Usage

From source file:at.ac.tuwien.dsg.depic.dataassetfunctionmanagement.util.TavernaRestAPI.java

public String callPutMethod(String xmlString) {
    String rs = "";

    try {// w  w  w .ja  va2s  .  c o m

        //HttpGet method = new HttpGet(url);
        StringEntity inputKeyspace = new StringEntity(xmlString);

        Logger.getLogger(TavernaRestAPI.class.getName()).log(Level.INFO, "Connection .. " + url);

        HttpPut request = new HttpPut(url);
        request.addHeader("content-type", "application/xml; charset=utf-8");
        //   request.addHeader("Accept", "application/xml, multipart/related");
        request.setEntity(inputKeyspace);

        HttpResponse methodResponse = this.getHttpClient().execute(request);

        int statusCode = methodResponse.getStatusLine().getStatusCode();

        Logger.getLogger(TavernaRestAPI.class.getName()).log(Level.INFO, "Status Code: " + statusCode);
        BufferedReader rd = new BufferedReader(new InputStreamReader(methodResponse.getEntity().getContent()));

        StringBuilder result = new StringBuilder();
        String line;
        while ((line = rd.readLine()) != null) {
            result.append(line);
        }

        rs = result.toString();
        // System.out.println("Response String: " + result.toString());
    } catch (Exception ex) {

    }
    return rs;
}

From source file:org.deviceconnect.android.profile.restful.test.FailAvailabilityProfileTestCase.java

/**
 * ?PUT?????./*from  ww w.j a v  a  2  s  .c o  m*/
 * <pre>
 * ?HTTP
 * Method: PUT
 * Path: /availability
 * </pre>
 * <pre>
 * ??
 * result?1???????
 * </pre>
 */
@Test
public void testGetAvailabilityInvalidMethodPut() {
    URIBuilder builder = TestURIBuilder.createURIBuilder();
    builder.setProfile(AvailabilityProfileConstants.PROFILE_NAME);
    try {
        HttpUriRequest request = new HttpPut(builder.toString());
        JSONObject root = sendRequest(request);
        assertResultError(ErrorCode.NOT_SUPPORT_ACTION.getCode(), root);
    } catch (JSONException e) {
        fail("Exception in JSONObject." + e.getMessage());
    }
}

From source file:nl.esciencecenter.octopus.webservice.mac.MacITCase.java

/**
 * Submit status to a callback server using MAC Access authentication.
 *
 * @throws URISyntaxException/*  w w  w . ja v  a2 s.co m*/
 * @throws ClientProtocolException
 * @throws IOException
 */
@Test
public void test() throws URISyntaxException, ClientProtocolException, IOException {
    Properties props = new Properties();
    props.load(MacITCase.class.getClassLoader().getResourceAsStream("integration.props"));

    URI url = new URI(props.getProperty("integration.callback.url"));

    // TODO throw better exception than NullPointerException when property can not be found.

    String state = "STOPPED";
    HttpPut request = new HttpPut(url);
    request.setEntity(new StringEntity(state));

    String mac_id = props.getProperty("integration.callback.id");
    String mac_key = props.getProperty("integration.callback.key");
    URI scope = new URI(url.getScheme(), null, url.getHost(), url.getPort(), null, null, null);
    ImmutableList<MacCredential> macs = ImmutableList.of(new MacCredential(mac_id, mac_key, scope));
    HttpClient httpClient = new DefaultHttpClient();
    httpClient = JobLauncherService.macifyHttpClient((AbstractHttpClient) httpClient, macs);

    HttpResponse response = httpClient.execute(request);

    assertEquals(200, response.getStatusLine().getStatusCode());
}

From source file:com.mongolduu.android.ng.misc.HttpConnector.java

public static String httpPUT(String url, String data) throws ClientProtocolException, IOException {
    HttpClient httpClient = createHttpClient();
    HttpPut httpPut = new HttpPut(url);
    httpPut.setEntity(new StringEntity(data));
    HttpResponse response = httpClient.execute(httpPut);
    if (response.getStatusLine().getStatusCode() >= 400) {
        throw new IOException("HTTP client error: " + response.getStatusLine().getStatusCode());
    }//from  w  w w.  j av  a 2  s. c  om
    return processEntity(response.getEntity());
}

From source file:org.codice.alliance.nsili.endpoint.requests.FtpDestinationSink.java

@Override
public void writeFile(InputStream fileData, long size, String name, String contentType,
        List<Metacard> metacards) throws IOException {
    CloseableHttpClient httpClient = null;
    String urlPath = protocol + "://" + fileLocation.host_name + ":" + port + "/" + fileLocation.path_name + "/"
            + name;//  w w w. j a v a2 s  .c o m

    LOGGER.debug("Writing ordered file to URL: {}", urlPath);

    try {
        HttpPut putMethod = new HttpPut(urlPath);
        putMethod.addHeader(HTTP.CONTENT_TYPE, contentType);
        HttpEntity httpEntity = new InputStreamEntity(fileData, size);
        putMethod.setEntity(httpEntity);

        if (StringUtils.isNotEmpty(fileLocation.user_name) && fileLocation.password != null) {
            CredentialsProvider credsProvider = new BasicCredentialsProvider();
            credsProvider.setCredentials(new AuthScope(fileLocation.host_name, port),
                    new UsernamePasswordCredentials(fileLocation.user_name, fileLocation.password));
            httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
        } else {
            httpClient = HttpClients.createDefault();
        }

        httpClient.execute(putMethod);
        fileData.close();
        putMethod.releaseConnection();
    } finally {
        if (httpClient != null) {
            httpClient.close();
        }
    }
}

From source file:com.anrisoftware.simplerest.core.AbstractSimplePutWorker.java

/**
 * Makes the request and sends the data and retrieves the response.
 *
 * @param entity/*from w w  w  .  jav a  2 s .c  om*/
 *            the {@link HttpEntity} entity.
 *
 * @return the response.
 *
 * @throws SimpleRestException
 */
public T sendData(HttpEntity entity) throws SimpleRestException {
    CloseableHttpClient httpclient = createHttpClient();
    HttpPut httpput = new HttpPut(requestUri);
    httpput.setEntity(entity);
    for (Header header : headers) {
        httpput.addHeader(header);
    }
    CloseableHttpResponse response = executeRequest(httpclient, httpput);
    StatusLine statusLine = response.getStatusLine();
    return parseResponse(response, httpput, statusLine);
}

From source file:org.deviceconnect.android.profile.restful.test.NormalMediaPlayerProfileTestCase.java

/**
 * ?????.// w ww  .j  a  v  a 2 s.co  m
 * <pre>
 * ?HTTP
 * Method: PUT
 * Path: /media_player/media?deviceId=xxxx&mediaId=xxxx
 * </pre>
 * <pre>
 * ??
 * result?0???????
 * </pre>
 */
public void testPutMedia() {
    URIBuilder builder = TestURIBuilder.createURIBuilder();
    builder.setProfile(MediaPlayerProfileConstants.PROFILE_NAME);
    builder.setAttribute(MediaPlayerProfileConstants.ATTRIBUTE_MEDIA);
    builder.addParameter(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN, getAccessToken());
    builder.addParameter(DConnectProfileConstants.PARAM_DEVICE_ID, getDeviceId());
    builder.addParameter(MediaPlayerProfileConstants.PARAM_MEDIA_ID, TEST_MEDIA_ID);
    try {
        HttpUriRequest request = new HttpPut(builder.toString());
        JSONObject response = sendRequest(request);
        assertResultOK(response);
    } catch (JSONException e) {
        fail("Exception in JSONObject." + e.getMessage());
    }
}

From source file:org.apache.hadoop.gateway.dispatch.HdfsDispatch.java

public void doPut(HttpServletRequest request, HttpServletResponse response)
        throws IOException, URISyntaxException {
    HttpEntity entity = createRequestEntity(request);
    URI requestUri = getDispatchUrl(request);

    auditor.audit(Action.DISPATCH, request.getRequestURI(), ResourceType.URI, ActionOutcome.UNAVAILABLE);
    if ("CREATE".equals(request.getParameter("op"))) {
        HttpPut clientRequest = new HttpPut(requestUri);
        HttpClient client = new DefaultHttpClient();
        HttpResponse clientResponse = client.execute(clientRequest);
        EntityUtils.consume(clientResponse.getEntity());
        if (clientResponse.getStatusLine().getStatusCode() == HttpStatus.TEMPORARY_REDIRECT_307) {
            Header locationHeader = clientResponse.getFirstHeader("Location");
            String location = locationHeader.getValue();
            clientRequest = new HttpPut(location);
            clientRequest.setEntity(entity);
            executeRequest(clientRequest, request, response);
        }//from   w  w  w.  j  av  a 2s  .c  om
    } else {
        HttpPut clientRequest = new HttpPut(requestUri);
        if (entity.getContentLength() > 0) {
            clientRequest.setEntity(entity);
        }
        executeRequest(clientRequest, request, response);
    }
}

From source file:onl.area51.filesystem.http.client.HttpUtils.java

public static void send(char[] path, Function<char[], String> remoteUri, Function<char[], Path> getPath,
        Supplier<String> userAgent) throws IOException {
    if (path == null || path.length == 0) {
        throw new FileNotFoundException("/");
    }//from w  w  w  .ja v  a  2 s  .c  o m

    String uri = remoteUri.apply(path);
    if (uri != null) {

        HttpEntity entity = new PathEntity(getPath.apply(path));

        LOG.log(Level.FINE, () -> "Sending " + uri);

        HttpPut put = new HttpPut(uri);
        put.setHeader(USER_AGENT, userAgent.get());
        put.setEntity(entity);

        try (CloseableHttpClient client = HttpClients.createDefault()) {
            try (CloseableHttpResponse response = client.execute(put)) {

                int returnCode = response.getStatusLine().getStatusCode();
                LOG.log(Level.FINE,
                        () -> "ReturnCode " + returnCode + ": " + response.getStatusLine().getReasonPhrase());
            }
        }
    }
}

From source file:com.microsoft.live.PutRequest.java

/**
 * Factory method override that constructs a HttpPut and adds a body to it.
 *
 * @return a HttpPut with the properly body added to it.
 *///  w w w  . j a  v  a 2  s  . c om
@Override
protected HttpUriRequest createHttpRequest() throws LiveOperationException {
    final HttpPut request = new HttpPut(this.requestUri.toString());

    request.setEntity(this.entity);

    return request;
}