Search in sources :

Example 91 with HttpDelete

use of org.apache.http.client.methods.HttpDelete in project sling by apache.

the class TopologyConnectorClient method disconnect.

/** Disconnect this connector **/
public void disconnect() {
    final String uri = connectorUrl.toString() + "." + clusterViewService.getSlingId() + ".json";
    if (logger.isDebugEnabled()) {
        logger.debug("disconnect: connectorUrl=" + connectorUrl + ", complete uri=" + uri);
    }
    if (lastInheritedAnnouncement != null) {
        announcementRegistry.unregisterAnnouncement(lastInheritedAnnouncement.getOwnerId());
    }
    final HttpClientContext clientContext = HttpClientContext.create();
    final CloseableHttpClient httpClient = createHttpClient();
    final HttpDelete deleteRequest = new HttpDelete(uri);
    // setting the connection timeout (idle connection, configured in seconds)
    deleteRequest.setConfig(RequestConfig.custom().setConnectTimeout(1000 * config.getSocketConnectTimeout()).build());
    try {
        String userInfo = connectorUrl.getUserInfo();
        if (userInfo != null) {
            Credentials c = new UsernamePasswordCredentials(userInfo);
            clientContext.getCredentialsProvider().setCredentials(new AuthScope(deleteRequest.getURI().getHost(), deleteRequest.getURI().getPort()), c);
        }
        requestValidator.trustMessage(deleteRequest, null);
        final CloseableHttpResponse response = httpClient.execute(deleteRequest, clientContext);
        if (logger.isDebugEnabled()) {
            logger.debug("disconnect: done. code=" + response.getStatusLine().getStatusCode() + " - " + response.getStatusLine().getReasonPhrase());
        }
    // ignoring the actual statuscode though as there's little we can
    // do about it after this point
    } catch (IOException e) {
        logger.warn("disconnect: got IOException: " + e);
    } catch (RuntimeException re) {
        logger.error("disconnect: got RuntimeException: " + re, re);
    } finally {
        deleteRequest.releaseConnection();
        try {
            httpClient.close();
        } catch (IOException e) {
            logger.error("disconnect: could not close httpClient: " + e, e);
        }
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpDelete(org.apache.http.client.methods.HttpDelete) AuthScope(org.apache.http.auth.AuthScope) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) IOException(java.io.IOException) Credentials(org.apache.http.auth.Credentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 92 with HttpDelete

use of org.apache.http.client.methods.HttpDelete in project lucene-solr by apache.

the class HttpSolrCall method remoteQuery.

private void remoteQuery(String coreUrl, HttpServletResponse resp) throws IOException {
    HttpRequestBase method = null;
    HttpEntity httpEntity = null;
    try {
        String urlstr = coreUrl + queryParams.toQueryString();
        boolean isPostOrPutRequest = "POST".equals(req.getMethod()) || "PUT".equals(req.getMethod());
        if ("GET".equals(req.getMethod())) {
            method = new HttpGet(urlstr);
        } else if ("HEAD".equals(req.getMethod())) {
            method = new HttpHead(urlstr);
        } else if (isPostOrPutRequest) {
            HttpEntityEnclosingRequestBase entityRequest = "POST".equals(req.getMethod()) ? new HttpPost(urlstr) : new HttpPut(urlstr);
            // Prevent close of container streams
            InputStream in = new CloseShieldInputStream(req.getInputStream());
            HttpEntity entity = new InputStreamEntity(in, req.getContentLength());
            entityRequest.setEntity(entity);
            method = entityRequest;
        } else if ("DELETE".equals(req.getMethod())) {
            method = new HttpDelete(urlstr);
        } else {
            throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unexpected method type: " + req.getMethod());
        }
        for (Enumeration<String> e = req.getHeaderNames(); e.hasMoreElements(); ) {
            String headerName = e.nextElement();
            if (!"host".equalsIgnoreCase(headerName) && !"authorization".equalsIgnoreCase(headerName) && !"accept".equalsIgnoreCase(headerName)) {
                method.addHeader(headerName, req.getHeader(headerName));
            }
        }
        // These headers not supported for HttpEntityEnclosingRequests
        if (method instanceof HttpEntityEnclosingRequest) {
            method.removeHeaders(TRANSFER_ENCODING_HEADER);
            method.removeHeaders(CONTENT_LENGTH_HEADER);
        }
        final HttpResponse response = solrDispatchFilter.httpClient.execute(method, HttpClientUtil.createNewHttpClientRequestContext());
        int httpStatus = response.getStatusLine().getStatusCode();
        httpEntity = response.getEntity();
        resp.setStatus(httpStatus);
        for (HeaderIterator responseHeaders = response.headerIterator(); responseHeaders.hasNext(); ) {
            Header header = responseHeaders.nextHeader();
            // encoding issues with Tomcat
            if (header != null && !header.getName().equalsIgnoreCase(TRANSFER_ENCODING_HEADER) && !header.getName().equalsIgnoreCase(CONNECTION_HEADER)) {
                resp.addHeader(header.getName(), header.getValue());
            }
        }
        if (httpEntity != null) {
            if (httpEntity.getContentEncoding() != null)
                resp.setCharacterEncoding(httpEntity.getContentEncoding().getValue());
            if (httpEntity.getContentType() != null)
                resp.setContentType(httpEntity.getContentType().getValue());
            InputStream is = httpEntity.getContent();
            OutputStream os = resp.getOutputStream();
            IOUtils.copyLarge(is, os);
        }
    } catch (IOException e) {
        sendError(new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Error trying to proxy request for url: " + coreUrl, e));
    } finally {
        Utils.consumeFully(httpEntity);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpEntityEnclosingRequestBase(org.apache.http.client.methods.HttpEntityEnclosingRequestBase) HttpEntity(org.apache.http.HttpEntity) HttpDelete(org.apache.http.client.methods.HttpDelete) CloseShieldInputStream(org.apache.commons.io.input.CloseShieldInputStream) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) OutputStream(java.io.OutputStream) CloseShieldOutputStream(org.apache.commons.io.output.CloseShieldOutputStream) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) HttpHead(org.apache.http.client.methods.HttpHead) HttpPut(org.apache.http.client.methods.HttpPut) InputStreamEntity(org.apache.http.entity.InputStreamEntity) Header(org.apache.http.Header) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) HeaderIterator(org.apache.http.HeaderIterator) CloseShieldInputStream(org.apache.commons.io.input.CloseShieldInputStream) SolrException(org.apache.solr.common.SolrException)

Example 93 with HttpDelete

use of org.apache.http.client.methods.HttpDelete in project stanbol by apache.

the class EntityhubTest method testEntityDeleteAll.

private void testEntityDeleteAll() throws IOException {
    Request request = builder.buildOtherRequest(new HttpDelete(builder.buildUrl("/entityhub/entity", "id", "*")));
    RequestExecutor re = executor.execute(request);
    re.assertStatus(200);
}
Also used : HttpDelete(org.apache.http.client.methods.HttpDelete) Request(org.apache.stanbol.commons.testing.http.Request) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) RequestExecutor(org.apache.stanbol.commons.testing.http.RequestExecutor)

Example 94 with HttpDelete

use of org.apache.http.client.methods.HttpDelete in project stanbol by apache.

the class ScopeTest method testActive.

@Test
public void testActive() throws Exception {
    RequestExecutor request;
    String tempActiveScopeUri = BASE_SCOPES_URI + "/" + getClass().getCanonicalName() + "-testActive-" + System.currentTimeMillis() + "-active";
    String tempInactiveScopeUri = BASE_SCOPES_URI + "/" + getClass().getCanonicalName() + "-testActive-" + System.currentTimeMillis() + "-inactive";
    // Scopes should not be there
    request = executor.execute(builder.buildGetRequest(tempActiveScopeUri).withHeader("Accept", KRFormat.TURTLE));
    request.assertStatus(404);
    log.info("Request: " + tempActiveScopeUri + " (should return 404) ... DONE");
    request = executor.execute(builder.buildGetRequest(tempInactiveScopeUri).withHeader("Accept", KRFormat.TURTLE));
    request.assertStatus(404);
    log.info("Request: " + tempInactiveScopeUri + " (should return 404) ... DONE");
    // Create scopes, only activate one
    executor.execute(builder.buildOtherRequest(new HttpPut(builder.buildUrl(tempActiveScopeUri + "?activate=true"))));
    log.info("PUT Request: " + tempActiveScopeUri + " ... DONE");
    executor.execute(builder.buildOtherRequest(new HttpPut(builder.buildUrl(tempInactiveScopeUri))));
    log.info("PUT Request: " + tempInactiveScopeUri + " ... DONE");
    // By default, we should only see the active scope
    executor.execute(builder.buildGetRequest(BASE_SCOPES_URI).withHeader("Accept", KRFormat.TURTLE)).assertStatus(200).assertContentRegexp(false, tempInactiveScopeUri + ">\\s+rdf:type\\s+<" + URI_SCOPE_CLASS + ">").assertContentRegexp(true, tempActiveScopeUri + ">\\s+rdf:type\\s+<" + URI_SCOPE_CLASS + ">");
    log.info("Request: " + BASE_SCOPES_URI + " ... DONE");
    // Using with-inactive we should see both scopes
    executor.execute(builder.buildGetRequest(BASE_SCOPES_URI + "?with-inactive=true").withHeader("Accept", KRFormat.TURTLE)).assertStatus(200).assertContentRegexp(true, tempInactiveScopeUri + ">\\s+rdf:type\\s+<" + URI_SCOPE_CLASS + ">").assertContentRegexp(true, tempActiveScopeUri + ">\\s+rdf:type\\s+<" + URI_SCOPE_CLASS + ">");
    log.info("Request: " + BASE_SCOPES_URI + " ... DONE");
    // Delete scopes
    executor.execute(builder.buildOtherRequest(new HttpDelete(builder.buildUrl(tempActiveScopeUri))));
    log.info("DELETE Request: " + tempActiveScopeUri + " ... DONE");
    executor.execute(builder.buildOtherRequest(new HttpDelete(builder.buildUrl(tempInactiveScopeUri))));
    log.info("DELETE Request: " + tempInactiveScopeUri + " ... DONE");
// We won't test here if deletion succeeded.
}
Also used : HttpDelete(org.apache.http.client.methods.HttpDelete) RequestExecutor(org.apache.stanbol.commons.testing.http.RequestExecutor) HttpPut(org.apache.http.client.methods.HttpPut) Test(org.junit.Test)

Example 95 with HttpDelete

use of org.apache.http.client.methods.HttpDelete in project stanbol by apache.

the class EntityhubTest method testEntityLookup.

@Test
public void testEntityLookup() throws IOException, JSONException {
    String uri = "http://dbpedia.org/resource/Paris";
    //first check that lookup without create returns 404
    RequestExecutor re = executor.execute(builder.buildGetRequest("/entityhub/lookup", "id", uri));
    re.assertStatus(404);
    //Now check that lookup with create does work
    re = executor.execute(builder.buildGetRequest("/entityhub/lookup", "id", uri, "create", "true"));
    re.assertStatus(200);
    JSONObject entity = assertEntity(re.getContent(), null, "entityhub");
    String ehUri = entity.optString("id", null);
    //try to retrieve the entity with the generated id
    re = executor.execute(builder.buildGetRequest("/entityhub/entity", "id", ehUri));
    re.assertStatus(200);
    assertEntity(re.getContent(), ehUri, "entityhub");
    //no try again to lookup the entity without create
    re = executor.execute(builder.buildGetRequest("/entityhub/lookup", "id", uri));
    re.assertStatus(200);
    assertEntity(re.getContent(), ehUri, "entityhub");
    //finally delete the entity
    re = executor.execute(builder.buildOtherRequest(new HttpDelete(builder.buildUrl("/entityhub/entity", "id", ehUri))));
    re.assertStatus(200);
}
Also used : JSONObject(org.codehaus.jettison.json.JSONObject) HttpDelete(org.apache.http.client.methods.HttpDelete) RequestExecutor(org.apache.stanbol.commons.testing.http.RequestExecutor) Test(org.junit.Test)

Aggregations

HttpDelete (org.apache.http.client.methods.HttpDelete)108 HttpResponse (org.apache.http.HttpResponse)25 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)22 Test (org.junit.Test)21 HttpGet (org.apache.http.client.methods.HttpGet)16 HttpPut (org.apache.http.client.methods.HttpPut)16 HttpPost (org.apache.http.client.methods.HttpPost)15 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)14 IOException (java.io.IOException)12 Deployment (org.activiti.engine.test.Deployment)12 ProcessInstance (org.activiti.engine.runtime.ProcessInstance)11 Task (org.activiti.engine.task.Task)9 StringEntity (org.apache.http.entity.StringEntity)9 URI (java.net.URI)7 RequestExecutor (org.apache.stanbol.commons.testing.http.RequestExecutor)7 URISyntaxException (java.net.URISyntaxException)6 Header (org.apache.http.Header)6 HttpEntity (org.apache.http.HttpEntity)6 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)6 User (org.activiti.engine.identity.User)5