Search in sources :

Example 26 with DeleteMethod

use of org.apache.commons.httpclient.methods.DeleteMethod in project xwiki-platform by xwiki.

the class AttachmentsResourceTest method testDELETEAttachment.

@Test
public void testDELETEAttachment() throws Exception {
    String attachmentName = String.format("%d.txt", System.currentTimeMillis());
    String attachmentURI = buildURIForThisPage(AttachmentResource.class, attachmentName);
    String content = "ATTACHMENT CONTENT";
    PutMethod putMethod = executePut(attachmentURI, content, MediaType.TEXT_PLAIN, TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword());
    Assert.assertEquals(getHttpMethodInfo(putMethod), HttpStatus.SC_CREATED, putMethod.getStatusCode());
    GetMethod getMethod = executeGet(attachmentURI);
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
    DeleteMethod deleteMethod = executeDelete(attachmentURI, TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword());
    Assert.assertEquals(getHttpMethodInfo(deleteMethod), HttpStatus.SC_NO_CONTENT, deleteMethod.getStatusCode());
    getMethod = executeGet(attachmentURI);
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_NOT_FOUND, getMethod.getStatusCode());
}
Also used : DeleteMethod(org.apache.commons.httpclient.methods.DeleteMethod) GetMethod(org.apache.commons.httpclient.methods.GetMethod) PutMethod(org.apache.commons.httpclient.methods.PutMethod) AbstractHttpTest(org.xwiki.test.rest.framework.AbstractHttpTest) Test(org.junit.Test)

Example 27 with DeleteMethod

use of org.apache.commons.httpclient.methods.DeleteMethod in project xwiki-platform by xwiki.

the class AttachmentsResourceTest method testDELETEAttachmentNoRights.

@Test
public void testDELETEAttachmentNoRights() throws Exception {
    String attachmentName = String.format("%d.txt", System.currentTimeMillis());
    String attachmentURI = buildURIForThisPage(AttachmentResource.class, attachmentName);
    String content = "ATTACHMENT CONTENT";
    PutMethod putMethod = executePut(attachmentURI, content, MediaType.TEXT_PLAIN, TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword());
    Assert.assertEquals(getHttpMethodInfo(putMethod), HttpStatus.SC_CREATED, putMethod.getStatusCode());
    DeleteMethod deleteMethod = executeDelete(attachmentURI);
    Assert.assertEquals(getHttpMethodInfo(deleteMethod), HttpStatus.SC_UNAUTHORIZED, deleteMethod.getStatusCode());
    GetMethod getMethod = executeGet(attachmentURI);
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
}
Also used : DeleteMethod(org.apache.commons.httpclient.methods.DeleteMethod) GetMethod(org.apache.commons.httpclient.methods.GetMethod) PutMethod(org.apache.commons.httpclient.methods.PutMethod) AbstractHttpTest(org.xwiki.test.rest.framework.AbstractHttpTest) Test(org.junit.Test)

Example 28 with DeleteMethod

use of org.apache.commons.httpclient.methods.DeleteMethod in project fabric8 by jboss-fuse.

the class ProxyServlet method doDelete.

/**
 * Performs an HTTP DELETE request
 *
 * @param httpServletRequest  The {@link javax.servlet.http.HttpServletRequest} object passed
 *                            in by the servlet engine representing the
 *                            client request to be proxied
 * @param httpServletResponse The {@link javax.servlet.http.HttpServletResponse} object by which
 *                            we can send a proxied response to the client
 */
@Override
public void doDelete(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException {
    ProxyDetails proxyDetails = createProxyDetails(httpServletRequest, httpServletResponse);
    if (!proxyDetails.isValid()) {
        noMappingFound(httpServletRequest, httpServletResponse);
    } else {
        DeleteMethod deleteMethodProxyRequest = new DeleteMethod(proxyDetails.getStringProxyURL());
        // Forward the request headers
        setProxyRequestHeaders(proxyDetails, httpServletRequest, deleteMethodProxyRequest);
        // Execute the proxy request
        executeProxyRequest(proxyDetails, deleteMethodProxyRequest, httpServletRequest, httpServletResponse);
    }
}
Also used : DeleteMethod(org.apache.commons.httpclient.methods.DeleteMethod)

Example 29 with DeleteMethod

use of org.apache.commons.httpclient.methods.DeleteMethod in project jaggery by wso2.

the class XMLHttpRequestHostObject method send.

private void send(Context cx, Object obj) throws ScriptException {
    final HttpMethodBase method;
    if ("GET".equalsIgnoreCase(methodName)) {
        method = new GetMethod(this.url);
    } else if ("HEAD".equalsIgnoreCase(methodName)) {
        method = new HeadMethod(this.url);
    } else if ("POST".equalsIgnoreCase(methodName)) {
        PostMethod post = new PostMethod(this.url);
        if (obj instanceof FormDataHostObject) {
            FormDataHostObject fd = ((FormDataHostObject) obj);
            List<Part> parts = new ArrayList<Part>();
            for (Map.Entry<String, String> entry : fd) {
                parts.add(new StringPart(entry.getKey(), entry.getValue()));
            }
            post.setRequestEntity(new MultipartRequestEntity(parts.toArray(new Part[parts.size()]), post.getParams()));
        } else {
            String content = getRequestContent(obj);
            if (content != null) {
                post.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(content.getBytes())));
            }
        }
        method = post;
    } else if ("PUT".equalsIgnoreCase(methodName)) {
        PutMethod put = new PutMethod(this.url);
        String content = getRequestContent(obj);
        if (content != null) {
            put.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(content.getBytes())));
        }
        method = put;
    } else if ("DELETE".equalsIgnoreCase(methodName)) {
        method = new DeleteMethod(this.url);
    } else if ("TRACE".equalsIgnoreCase(methodName)) {
        method = new TraceMethod(this.url);
    } else if ("OPTIONS".equalsIgnoreCase(methodName)) {
        method = new OptionsMethod(this.url);
    } else {
        throw new ScriptException("Unknown HTTP method : " + methodName);
    }
    for (Header header : requestHeaders) {
        method.addRequestHeader(header);
    }
    if (username != null) {
        httpClient.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
    }
    this.method = method;
    final XMLHttpRequestHostObject xhr = this;
    if (async) {
        updateReadyState(cx, xhr, LOADING);
        final ContextFactory factory = cx.getFactory();
        final ExecutorService es = Executors.newSingleThreadExecutor();
        es.submit(new Callable() {

            public Object call() throws Exception {
                Context ctx = RhinoEngine.enterContext(factory);
                try {
                    executeRequest(ctx, xhr);
                } catch (ScriptException e) {
                    log.error(e.getMessage(), e);
                } finally {
                    es.shutdown();
                    RhinoEngine.exitContext();
                }
                return null;
            }
        });
    } else {
        executeRequest(cx, xhr);
    }
}
Also used : InputStreamRequestEntity(org.apache.commons.httpclient.methods.InputStreamRequestEntity) PostMethod(org.apache.commons.httpclient.methods.PostMethod) ArrayList(java.util.ArrayList) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) Callable(java.util.concurrent.Callable) ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) HeadMethod(org.apache.commons.httpclient.methods.HeadMethod) OptionsMethod(org.apache.commons.httpclient.methods.OptionsMethod) Context(org.mozilla.javascript.Context) DeleteMethod(org.apache.commons.httpclient.methods.DeleteMethod) HttpMethodBase(org.apache.commons.httpclient.HttpMethodBase) TraceMethod(org.apache.commons.httpclient.methods.TraceMethod) MultipartRequestEntity(org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity) IOException(java.io.IOException) ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) ContextFactory(org.mozilla.javascript.ContextFactory) Header(org.apache.commons.httpclient.Header) ByteArrayInputStream(java.io.ByteArrayInputStream) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) Part(org.apache.commons.httpclient.methods.multipart.Part) GetMethod(org.apache.commons.httpclient.methods.GetMethod) ExecutorService(java.util.concurrent.ExecutorService) PutMethod(org.apache.commons.httpclient.methods.PutMethod) ScriptableObject(org.mozilla.javascript.ScriptableObject) Map(java.util.Map)

Example 30 with DeleteMethod

use of org.apache.commons.httpclient.methods.DeleteMethod in project application by collectionspace.

the class ServicesConnection method createMethod.

private HttpMethod createMethod(RequestMethod method, String uri, InputStream data) throws ConnectionException {
    uri = prepend_base(uri);
    if (uri == null)
        throw new ConnectionException("URI must not be null");
    // Extract QP's
    int qp_start = uri.indexOf('?');
    String qps = null;
    if (qp_start != -1) {
        qps = uri.substring(qp_start + 1);
        uri = uri.substring(0, qp_start);
    }
    HttpMethod out = null;
    switch(method) {
        case POST:
            {
                out = new PostMethod(uri);
                if (data != null)
                    ((PostMethod) out).setRequestEntity(new InputStreamRequestEntity(data));
                break;
            }
        case PUT:
            {
                out = new PutMethod(uri);
                if (data != null)
                    ((PutMethod) out).setRequestEntity(new InputStreamRequestEntity(data));
                break;
            }
        case GET:
            out = new GetMethod(uri);
            break;
        case DELETE:
            out = new DeleteMethod(uri);
            break;
        default:
            throw new ConnectionException("Unsupported method " + method, 0, uri);
    }
    if (qps != null)
        out.setQueryString(qps);
    out.setDoAuthentication(true);
    return out;
}
Also used : InputStreamRequestEntity(org.apache.commons.httpclient.methods.InputStreamRequestEntity) 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) HttpMethod(org.apache.commons.httpclient.HttpMethod)

Aggregations

DeleteMethod (org.apache.commons.httpclient.methods.DeleteMethod)34 GetMethod (org.apache.commons.httpclient.methods.GetMethod)16 PutMethod (org.apache.commons.httpclient.methods.PutMethod)12 Test (org.junit.Test)8 PostMethod (org.apache.commons.httpclient.methods.PostMethod)7 AbstractHttpTest (org.xwiki.test.rest.framework.AbstractHttpTest)7 IOException (java.io.IOException)6 HttpMethod (org.apache.commons.httpclient.HttpMethod)6 HttpClient (org.apache.commons.httpclient.HttpClient)5 HttpMethodBase (org.apache.commons.httpclient.HttpMethodBase)4 StringRequestEntity (org.apache.commons.httpclient.methods.StringRequestEntity)4 HeadMethod (org.apache.commons.httpclient.methods.HeadMethod)3 InputStreamRequestEntity (org.apache.commons.httpclient.methods.InputStreamRequestEntity)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 ExecutorService (java.util.concurrent.ExecutorService)2 Header (org.apache.commons.httpclient.Header)2 URIException (org.apache.commons.httpclient.URIException)2 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)2 ByteArrayRequestEntity (org.apache.commons.httpclient.methods.ByteArrayRequestEntity)2