Search in sources :

Example 91 with PutMethod

use of org.apache.commons.httpclient.methods.PutMethod in project android by nextcloud.

the class NotificationExecuteActionTask method doInBackground.

@Override
protected Boolean doInBackground(Action... actions) {
    HttpMethod method;
    Action action = actions[0];
    switch(action.type) {
        case "GET":
            method = new GetMethod(action.link);
            break;
        case "POST":
            method = new Utf8PostMethod(action.link);
            break;
        case "DELETE":
            method = new DeleteMethod(action.link);
            break;
        case "PUT":
            method = new PutMethod(action.link);
            break;
        default:
            // do nothing
            return Boolean.FALSE;
    }
    method.setRequestHeader(RemoteOperation.OCS_API_HEADER, RemoteOperation.OCS_API_HEADER_VALUE);
    int status;
    try {
        status = client.executeMethod(method);
    } catch (IOException e) {
        Log_OC.e(this, "Execution of notification action failed: " + e);
        return Boolean.FALSE;
    } finally {
        method.releaseConnection();
    }
    return status == HttpStatus.SC_OK || status == HttpStatus.SC_ACCEPTED;
}
Also used : Action(com.owncloud.android.lib.resources.notifications.models.Action) DeleteMethod(org.apache.commons.httpclient.methods.DeleteMethod) Utf8PostMethod(org.apache.commons.httpclient.methods.Utf8PostMethod) GetMethod(org.apache.commons.httpclient.methods.GetMethod) PutMethod(org.apache.commons.httpclient.methods.PutMethod) IOException(java.io.IOException) HttpMethod(org.apache.commons.httpclient.HttpMethod)

Example 92 with PutMethod

use of org.apache.commons.httpclient.methods.PutMethod in project alfresco-repository by Alfresco.

the class BaseWebScriptTest method sendRemoteRequest.

/**
 * Send Remote Request to stand-alone Web Script Server
 *
 * @param req Request
 * @param expectedStatus int
 * @return response
 * @throws IOException
 */
protected Response sendRemoteRequest(Request req, int expectedStatus) throws IOException {
    String uri = req.getFullUri();
    if (!uri.startsWith("http")) {
        uri = remoteServer.baseAddress + uri;
    }
    // construct method
    HttpMethod httpMethod = null;
    String method = req.getMethod();
    if (method.equalsIgnoreCase("GET")) {
        GetMethod get = new GetMethod(req.getFullUri());
        httpMethod = get;
    } else if (method.equalsIgnoreCase("POST")) {
        PostMethod post = new PostMethod(req.getFullUri());
        post.setRequestEntity(new ByteArrayRequestEntity(req.getBody(), req.getType()));
        httpMethod = post;
    } else if (method.equalsIgnoreCase("PATCH")) {
        PatchMethod post = new PatchMethod(req.getFullUri());
        post.setRequestEntity(new ByteArrayRequestEntity(req.getBody(), req.getType()));
        httpMethod = post;
    } else if (method.equalsIgnoreCase("PUT")) {
        PutMethod put = new PutMethod(req.getFullUri());
        put.setRequestEntity(new ByteArrayRequestEntity(req.getBody(), req.getType()));
        httpMethod = put;
    } else if (method.equalsIgnoreCase("DELETE")) {
        DeleteMethod del = new DeleteMethod(req.getFullUri());
        httpMethod = del;
    } else {
        throw new AlfrescoRuntimeException("Http Method " + method + " not supported");
    }
    if (req.getHeaders() != null) {
        for (Map.Entry<String, String> header : req.getHeaders().entrySet()) {
            httpMethod.setRequestHeader(header.getKey(), header.getValue());
        }
    }
    // execute method
    httpClient.executeMethod(httpMethod);
    return new HttpMethodResponse(httpMethod);
}
Also used : DeleteMethod(org.apache.commons.httpclient.methods.DeleteMethod) PostMethod(org.apache.commons.httpclient.methods.PostMethod) GetMethod(org.apache.commons.httpclient.methods.GetMethod) PutMethod(org.apache.commons.httpclient.methods.PutMethod) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) Map(java.util.Map) HttpMethod(org.apache.commons.httpclient.HttpMethod) ByteArrayRequestEntity(org.apache.commons.httpclient.methods.ByteArrayRequestEntity)

Example 93 with PutMethod

use of org.apache.commons.httpclient.methods.PutMethod in project Spark by igniterealtime.

the class ChatRoomDecorator method uploadFile.

private void uploadFile(File file, UploadRequest response, ChatRoom room, Message.Type type) {
    Log.warning("uploadFile request " + room.getBareJid() + " " + response.putUrl);
    URLConnection urlconnection = null;
    try {
        PutMethod put = new PutMethod(response.putUrl);
        int port = put.getURI().getPort();
        if (port > 0) {
            Protocol.registerProtocol("https", new Protocol("https", new EasySSLProtocolSocketFactory(), port));
        }
        HttpClient client = new HttpClient();
        RequestEntity entity = new FileRequestEntity(file, "application/binary");
        put.setRequestEntity(entity);
        put.setRequestHeader("User-Agent", "Spark HttpFileUpload");
        client.executeMethod(put);
        int statusCode = put.getStatusCode();
        String responseBody = put.getResponseBodyAsString();
        Log.warning("uploadFile response " + statusCode + " " + responseBody);
        if ((statusCode >= 200) && (statusCode <= 202)) {
            broadcastUploadUrl(room.getBareJid(), response.getUrl, type);
        }
    } catch (Exception e) {
        Log.error("uploadFile error", e);
    }
}
Also used : FileRequestEntity(org.apache.commons.httpclient.methods.FileRequestEntity) HttpClient(org.apache.commons.httpclient.HttpClient) PutMethod(org.apache.commons.httpclient.methods.PutMethod) Protocol(org.apache.commons.httpclient.protocol.Protocol) FileRequestEntity(org.apache.commons.httpclient.methods.FileRequestEntity) RequestEntity(org.apache.commons.httpclient.methods.RequestEntity) EasySSLProtocolSocketFactory(org.jivesoftware.sparkimpl.updater.EasySSLProtocolSocketFactory)

Example 94 with PutMethod

use of org.apache.commons.httpclient.methods.PutMethod in project jaxrs-api by eclipse-ee4j.

the class MethodFactory method getInstance.

/*
   * public methods
   * ========================================================================
   */
/**
 * Returns the approriate request method based on the provided request string.
 * The request must be in the format of METHOD URI_PATH HTTP_VERSION, i.e. GET
 * /index.jsp HTTP/1.1.
 *
 * @return HttpMethod based in request.
 */
public static HttpMethod getInstance(String request) {
    StringTokenizer st = new StringTokenizer(request);
    String method;
    String query = null;
    String uri;
    String version;
    try {
        method = st.nextToken();
        uri = TS_URL.getRequest(st.nextToken());
        version = st.nextToken();
    } catch (NoSuchElementException nsee) {
        throw new IllegalArgumentException("Request provided: " + request + " is malformed.");
    }
    // check to see if there is a query string appended
    // to the URI
    int queryStart = uri.indexOf('?');
    if (queryStart != -1) {
        query = uri.substring(queryStart + 1);
        uri = uri.substring(0, queryStart);
    }
    HttpMethodBase req;
    if (method.equals(GET_METHOD)) {
        req = new GetMethod(uri);
    } else if (method.equals(POST_METHOD)) {
        req = new PostMethod(uri);
    } else if (method.equals(PUT_METHOD)) {
        req = new PutMethod(uri);
    } else if (method.equals(DELETE_METHOD)) {
        req = new DeleteMethod(uri);
    } else if (method.equals(HEAD_METHOD)) {
        req = new HeadMethod(uri);
    } else if (method.equals(OPTIONS_METHOD)) {
        req = new OptionsMethod(uri);
    } else {
        throw new IllegalArgumentException("Invalid method: " + method);
    }
    setHttpVersion(version, req);
    if (query != null) {
        req.setQueryString(query);
    }
    return req;
}
Also used : StringTokenizer(java.util.StringTokenizer) DeleteMethod(org.apache.commons.httpclient.methods.DeleteMethod) HeadMethod(org.apache.commons.httpclient.methods.HeadMethod) OptionsMethod(org.apache.commons.httpclient.methods.OptionsMethod) HttpMethodBase(org.apache.commons.httpclient.HttpMethodBase) PostMethod(org.apache.commons.httpclient.methods.PostMethod) GetMethod(org.apache.commons.httpclient.methods.GetMethod) PutMethod(org.apache.commons.httpclient.methods.PutMethod) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

PutMethod (org.apache.commons.httpclient.methods.PutMethod)94 Test (org.junit.Test)49 GetMethod (org.apache.commons.httpclient.methods.GetMethod)29 AbstractHttpTest (org.xwiki.test.rest.framework.AbstractHttpTest)21 StringRequestEntity (org.apache.commons.httpclient.methods.StringRequestEntity)19 Page (org.xwiki.rest.model.jaxb.Page)15 HttpClient (org.apache.commons.httpclient.HttpClient)14 IOException (java.io.IOException)13 RequestEntity (org.apache.commons.httpclient.methods.RequestEntity)13 DeleteMethod (org.apache.commons.httpclient.methods.DeleteMethod)12 PostMethod (org.apache.commons.httpclient.methods.PostMethod)10 InputStreamRequestEntity (org.apache.commons.httpclient.methods.InputStreamRequestEntity)7 DeleteMethod (org.apache.jackrabbit.webdav.client.methods.DeleteMethod)7 Link (org.xwiki.rest.model.jaxb.Link)7 Header (org.apache.commons.httpclient.Header)6 HttpMethod (org.apache.commons.httpclient.HttpMethod)6 FileRequestEntity (org.apache.commons.httpclient.methods.FileRequestEntity)6 File (java.io.File)5 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)5 Object (org.xwiki.rest.model.jaxb.Object)5