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);
}
}
}
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);
}
}
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);
}
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.
}
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);
}
Aggregations