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:com.selene.volley.stack.HttpClientStack.java

/**
 * Creates the appropriate subclass of HttpUriRequest for passed in request.
 *//*from  ww  w.  java 2 s .  c o m*/
@SuppressWarnings("deprecation")
/* protected */ static HttpUriRequest createHttpRequest(Request<?> request,
        Map<String, String> additionalHeaders) throws AuthFailureError {
    switch (request.getMethod()) {
    case Method.DEPRECATED_GET_OR_POST:
        // This is the deprecated way that needs to be handled for backwards
        // compatibility.
        // If the request's post body is null, then the assumption is that
        // the request is
        // GET. Otherwise, it is assumed that the request is a POST.
        byte[] postBody = request.getPostBody();
        if (postBody != null) {
            HttpPost postRequest = new HttpPost(request.getUrl());
            postRequest.addHeader(HEADER_CONTENT_TYPE, request.getPostBodyContentType());
            HttpEntity entity;
            entity = new ByteArrayEntity(postBody);
            postRequest.setEntity(entity);
            return postRequest;
        } else {
            return new HttpGet(request.getUrl());
        }
    case Method.GET:
        return new HttpGet(request.getUrl());
    case Method.DELETE:
        return new HttpDelete(request.getUrl());
    case Method.POST: {
        HttpPost postRequest = new HttpPost(request.getUrl());
        postRequest.addHeader(HTTP.CONTENT_TYPE, request.getBodyContentType());
        setEntityIfNonEmptyBody(postRequest, request);
        return postRequest;
    }
    case Method.PUT: {
        HttpPut putRequest = new HttpPut(request.getUrl());
        putRequest.addHeader(HTTP.CONTENT_TYPE, request.getBodyContentType());
        setEntityIfNonEmptyBody(putRequest, request);
        return putRequest;
    }
    case Method.HEAD:
        return new HttpHead(request.getUrl());
    case Method.OPTIONS:
        return new HttpOptions(request.getUrl());
    case Method.TRACE:
        return new HttpTrace(request.getUrl());
    case Method.PATCH: {
        HttpPatch patchRequest = new HttpPatch(request.getUrl());
        patchRequest.addHeader(HTTP.CONTENT_TYPE, request.getBodyContentType());
        setEntityIfNonEmptyBody(patchRequest, request);
        return patchRequest;
    }
    default:
        throw new IllegalStateException("Unknown request method.");
    }
}

From source file:com.derson.pumelo.network.volley.toolbox.HttpClientStack.java

/**
 * Creates the appropriate subclass of HttpUriRequest for passed in request.
 *///from   ww w. j  av a  2 s  . c  o  m
@SuppressWarnings("deprecation")
/* protected */ static HttpUriRequest createHttpRequest(Request<?> request,
        Map<String, String> additionalHeaders) throws AuthFailureError {
    switch (request.getMethod()) {
    case Request.Method.DEPRECATED_GET_OR_POST: {
        // This is the deprecated way that needs to be handled for backwards compatibility.
        // If the request's post body is null, then the assumption is that the request is
        // GET.  Otherwise, it is assumed that the request is a POST.
        byte[] postBody = request.getPostBody();
        if (postBody != null) {
            HttpPost postRequest = new HttpPost(request.getUrl());
            postRequest.addHeader(HEADER_CONTENT_TYPE, request.getPostBodyContentType());
            HttpEntity entity;
            entity = new ByteArrayEntity(postBody);
            postRequest.setEntity(entity);
            return postRequest;
        } else {
            return new HttpGet(request.getUrl());
        }
    }

    case Request.Method.GET:
        return new HttpGet(request.getUrl());

    case Request.Method.DELETE:
        return new HttpDelete(request.getUrl());
    case Request.Method.POST: {
        HttpPost postRequest = new HttpPost(request.getUrl());
        postRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
        setEntityIfNonEmptyBody(postRequest, request);
        return postRequest;
    }
    case Request.Method.PUT: {
        HttpPut putRequest = new HttpPut(request.getUrl());
        putRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
        setEntityIfNonEmptyBody(putRequest, request);
        return putRequest;
    }
    case Request.Method.HEAD:
        return new HttpHead(request.getUrl());
    case Request.Method.OPTIONS:
        return new HttpOptions(request.getUrl());
    case Request.Method.TRACE:
        return new HttpTrace(request.getUrl());
    case Request.Method.PATCH: {
        HttpPatch patchRequest = new HttpPatch(request.getUrl());
        patchRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
        setEntityIfNonEmptyBody(patchRequest, request);
        return patchRequest;
    }
    default:
        throw new IllegalStateException("Unknown request method.");
    }
}

From source file:io.fabric8.itests.paxexam.basic.FabricMavenProxyTest.java

@Test
public void testUpload() throws Exception {
    String featureLocation = System.getProperty("feature.location");
    System.out.println("Testing with feature from:" + featureLocation);
    System.out.println(executeCommand("fabric:create -n --wait-for-provisioning"));
    //System.out.println(executeCommand("shell:info"));
    //System.out.println(executeCommand("fabric:info"));
    //System.out.println(executeCommand("fabric:profile-list"));

    ServiceProxy<FabricService> fabricProxy = ServiceProxy.createServiceProxy(bundleContext,
            FabricService.class);
    try {//from w  w  w .ja  v  a 2 s  . c om
        Set<ContainerProxy> containers = ContainerBuilder.create(fabricProxy, 2).withName("maven")
                .withProfiles("fabric").assertProvisioningResult().build();
        try {
            List<String> uploadUrls = new ArrayList<String>();
            ServiceProxy<CuratorFramework> curatorProxy = ServiceProxy.createServiceProxy(bundleContext,
                    CuratorFramework.class);
            try {
                CuratorFramework curator = curatorProxy.getService();
                List<String> children = ZooKeeperUtils.getChildren(curator,
                        ZkPath.MAVEN_PROXY.getPath("upload"));
                for (String child : children) {
                    String uploadeUrl = ZooKeeperUtils.getSubstitutedPath(curator,
                            ZkPath.MAVEN_PROXY.getPath("upload") + "/" + child);
                    uploadUrls.add(uploadeUrl);
                }
            } finally {
                curatorProxy.close();
            }
            //Pick a random maven proxy from the list.
            Random random = new Random();
            int index = random.nextInt(uploadUrls.size());
            String targetUrl = uploadUrls.get(index);

            String uploadUrl = targetUrl + "itest/itest/1.0/itest-1.0-features.xml";
            System.out.println("Using URI: " + uploadUrl);
            DefaultHttpClient client = new DefaultHttpClient();
            HttpPut put = new HttpPut(uploadUrl);
            client.getCredentialsProvider().setCredentials(AuthScope.ANY,
                    new UsernamePasswordCredentials("admin", "admin"));

            FileNIOEntity entity = new FileNIOEntity(new File(featureLocation), "text/xml");
            put.setEntity(entity);
            HttpResponse response = client.execute(put);
            System.out.println("Response:" + response.getStatusLine());
            Assert.assertTrue(response.getStatusLine().getStatusCode() == 200
                    || response.getStatusLine().getStatusCode() == 202);

            System.out.println(executeCommand(
                    "fabric:profile-edit --repositories mvn:itest/itest/1.0/xml/features default"));
            System.out.println(executeCommand("fabric:profile-edit --features example-cbr default"));
            Provision.containerStatus(containers, PROVISION_TIMEOUT);
        } finally {
            ContainerBuilder.destroy(containers);
        }
    } finally {
        fabricProxy.close();
    }
}

From source file:ch.ifocusit.livingdoc.plugin.publish.confluence.client.HttpRequestFactory.java

HttpPut updatePageRequest(String contentId, String ancestorId, String title, String content, int newVersion) {
    assertMandatoryParameter(isNotBlank(contentId), "contentId");
    assertMandatoryParameter(isNotBlank(ancestorId), "ancestorId");
    assertMandatoryParameter(isNotBlank(title), "title");

    PagePayload pagePayload = pagePayloadBuilder().ancestorId(ancestorId).title(title).content(content)
            .version(newVersion).build();

    HttpPut updatePageRequest = new HttpPut(this.confluenceRestApiEndpoint + "/content/" + contentId);
    updatePageRequest.setEntity(httpEntityWithJsonPayload(pagePayload));
    updatePageRequest.addHeader(APPLICATION_JSON_UTF8_HEADER);

    return updatePageRequest;
}

From source file:com.iframe.source.publics.http.volley.toolbox.HttpClientStack.java

/**
 * Creates the appropriate subclass of HttpUriRequest for passed in request.
 *//*from   www.j av a  2s.c  o  m*/
@SuppressWarnings("deprecation")
/* protected */static HttpUriRequest createHttpRequest(Request<?> request,
        Map<String, String> additionalHeaders) throws AuthFailureError {
    switch (request.getMethod()) {
    case Method.DEPRECATED_GET_OR_POST: {
        // This is the deprecated way that needs to be handled for backwards compatibility.
        // If the request's post body is null, then the assumption is that the request is
        // GET.  Otherwise, it is assumed that the request is a POST.
        byte[] postBody = request.getPostBody();
        if (postBody != null) {
            HttpPost postRequest = new HttpPost(request.getUrl());
            postRequest.addHeader(HEADER_CONTENT_TYPE, request.getPostBodyContentType());
            HttpEntity entity;
            entity = new ByteArrayEntity(postBody);
            postRequest.setEntity(entity);
            return postRequest;
        } else {
            return new HttpGet(request.getUrl());
        }
    }
    case Method.GET:
        return new HttpGet(request.getUrl());
    case Method.DELETE:
        return new HttpDelete(request.getUrl());
    case Method.POST: {
        HttpPost postRequest = new HttpPost(request.getUrl());
        postRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
        setEntityIfNonEmptyBody(postRequest, request);
        return postRequest;
    }
    case Method.PUT: {
        HttpPut putRequest = new HttpPut(request.getUrl());
        putRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
        setEntityIfNonEmptyBody(putRequest, request);
        return putRequest;
    }
    case Method.HEAD:
        return new HttpHead(request.getUrl());
    case Method.OPTIONS:
        return new HttpOptions(request.getUrl());
    case Method.TRACE:
        return new HttpTrace(request.getUrl());
    case Method.PATCH: {
        HttpPatch patchRequest = new HttpPatch(request.getUrl());
        patchRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
        setEntityIfNonEmptyBody(patchRequest, request);
        return patchRequest;
    }
    default:
        throw new IllegalStateException("Unknown request method.");
    }
}

From source file:com.sap.core.odata.fit.basic.RequestContentTypeTest.java

@Test
public void xmlContentType() throws Exception {
    HttpPut put = new HttpPut(URI.create(getEndpoint().toString() + "Rooms('1')"));
    put.setHeader(HttpHeaders.CONTENT_TYPE, "xml");
    final HttpResponse response = getHttpClient().execute(put);
    assertEquals(HttpStatusCodes.UNSUPPORTED_MEDIA_TYPE.getStatusCode(),
            response.getStatusLine().getStatusCode());
}

From source file:com.spotworld.spotapp.widget.utils.volley.toolbox.HttpClientStack.java

/**
 * Creates the appropriate subclass of HttpUriRequest for passed in request.
 *///from  www.j av  a  2 s . c  o  m
@SuppressWarnings("deprecation")
/* protected */static HttpUriRequest createHttpRequest(Request<?> request,
        Map<String, String> additionalHeaders) throws AuthFailureError {
    switch (request.getMethod()) {
    case Method.DEPRECATED_GET_OR_POST: {
        // This is the deprecated way that needs to be handled for backwards compatibility.
        // If the request's post body is null, then the assumption is that the request is
        // GET. Otherwise, it is assumed that the request is a POST.
        byte[] postBody = request.getPostBody();
        if (postBody != null) {
            HttpPost postRequest = new HttpPost(request.getUrl());
            postRequest.addHeader(HEADER_CONTENT_TYPE, request.getPostBodyContentType());
            HttpEntity entity;
            entity = new ByteArrayEntity(postBody);
            postRequest.setEntity(entity);
            return postRequest;
        } else {
            return new HttpGet(request.getUrl());
        }
    }
    case Method.GET:
        return new HttpGet(request.getUrl());
    case Method.DELETE:
        return new HttpDelete(request.getUrl());
    case Method.POST: {
        HttpPost postRequest = new HttpPost(request.getUrl());
        postRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
        setEntityIfNonEmptyBody(postRequest, request);
        return postRequest;
    }
    case Method.PUT: {
        HttpPut putRequest = new HttpPut(request.getUrl());
        putRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
        setEntityIfNonEmptyBody(putRequest, request);
        return putRequest;
    }
    case Method.HEAD:
        return new HttpHead(request.getUrl());
    case Method.OPTIONS:
        return new HttpOptions(request.getUrl());
    case Method.TRACE:
        return new HttpTrace(request.getUrl());
    case Method.PATCH: {
        HttpPatch patchRequest = new HttpPatch(request.getUrl());
        patchRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
        setEntityIfNonEmptyBody(patchRequest, request);
        return patchRequest;
    }
    default:
        throw new IllegalStateException("Unknown request method.");
    }
}

From source file:org.restcomm.connect.java.sdk.http.HttpClient.java

public String connect(final Request request) {
    String responseString = null;
    try {/*from w ww  .  j av a 2s  .  c o m*/

        if (request.getMethod().toString() == "GET") {
            httpget = new HttpGet(request.getUrl());
            httpget.setHeader("Authorization", "Basic " + new String(this.credentials, StandardCharsets.UTF_8));
            response = httpclient.execute(httpget);
        } else if (request.getMethod().toString() == "POST") {
            httppost = new HttpPost(request.getUrl());
            httppost.setHeader("Authorization", "Basic " + new String(credentials, StandardCharsets.UTF_8));
            httppost.setEntity(new UrlEncodedFormEntity(request.PostParameters));
            response = httpclient.execute(httppost);

            /*BufferedReader rd = new BufferedReader(new InputStreamReader(
                       response.getEntity().getContent()));
                    
               String line = "";
               while ((line = rd.readLine()) != null) {
                   System.out.println(line);
                   if (line.startsWith("Auth=")) {
                       String key = line.substring(5);
                       // do something with the key
                   }
                    
               }*/
        } else if (request.getMethod().toString() == "PUT") {
            httpput = new HttpPut(request.getUrl());
            httpput.setHeader("Authorization", "Basic " + new String(credentials, StandardCharsets.UTF_8));
            httpput.setEntity(new UrlEncodedFormEntity(request.PostParameters));
            response = httpclient.execute(httpput);
            /*BufferedReader rd = new BufferedReader(new InputStreamReader(
                 response.getEntity().getContent()));
                    
            String line = "";
            while ((line = rd.readLine()) != null) {
             System.out.println(line);
             if (line.startsWith("Auth=")) {
                 String key = line.substring(5);
                 // do something with the key
             }
                    
            }*/
        } else if (request.getMethod().toString() == "DELETE") {
            httpdelete = new HttpDelete(request.getUrl());
            httpdelete.setHeader("Authorization", "Basic " + new String(credentials, StandardCharsets.UTF_8));
            response = httpclient.execute(httpdelete);
        }

        ExceptionHandler Handle = new ExceptionHandler(response);

        this.StatusCode = response.getStatusLine().getStatusCode();

        responseString = EntityUtils.toString(response.getEntity());
        response.close();
        httpclient.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return responseString;
}

From source file:com.pmi.restlet.ext.httpclient.internal.HttpMethodCall.java

/**
 * Constructor./*from  www .  ja v  a2 s.co m*/
 *
 * @param helper
 *            The parent HTTP client helper.
 * @param method
 *            The method name.
 * @param requestUri
 *            The request URI.
 * @param hasEntity
 *            Indicates if the call will have an entity to send to the
 *            server.
 * @throws IOException
 */
public HttpMethodCall(HttpClientHelper helper, final String method, final String requestUri, boolean hasEntity)
        throws IOException {
    super(helper, method, requestUri);
    this.clientHelper = helper;

    if (requestUri.startsWith("http")) {
        if (method.equalsIgnoreCase(Method.GET.getName())) {
            this.httpRequest = new HttpGet(requestUri);
        } else if (method.equalsIgnoreCase(Method.POST.getName())) {
            this.httpRequest = new HttpPost(requestUri);
        } else if (method.equalsIgnoreCase(Method.PUT.getName())) {
            this.httpRequest = new HttpPut(requestUri);
        } else if (method.equalsIgnoreCase(Method.HEAD.getName())) {
            this.httpRequest = new HttpHead(requestUri);
        } else if (method.equalsIgnoreCase(Method.DELETE.getName())) {
            this.httpRequest = new HttpDelete(requestUri);
        } else if (method.equalsIgnoreCase(Method.OPTIONS.getName())) {
            this.httpRequest = new HttpOptions(requestUri);
        } else if (method.equalsIgnoreCase(Method.TRACE.getName())) {
            this.httpRequest = new HttpTrace(requestUri);
        } else {
            this.httpRequest = new HttpEntityEnclosingRequestBase() {

                @Override
                public String getMethod() {
                    return method;
                }

                @Override
                public URI getURI() {
                    try {
                        return new URI(requestUri);
                    } catch (URISyntaxException e) {
                        getLogger().log(Level.WARNING, "Invalid URI syntax", e);
                        return null;
                    }
                }
            };
        }

        this.responseHeadersAdded = false;
        setConfidential(this.httpRequest.getURI().getScheme().equalsIgnoreCase(Protocol.HTTPS.getSchemeName()));
    } else {
        throw new IllegalArgumentException("Only HTTP or HTTPS resource URIs are allowed here");
    }
}