use of org.apache.http.client.methods.HttpRequestBase in project lucene-solr by apache.
the class CacheHeaderTest method doETag.
// test ETag
@Override
protected void doETag(String method) throws Exception {
HttpRequestBase get = getSelectMethod(method);
HttpResponse response = getClient().execute(get);
checkResponseBody(method, response);
assertEquals("Got no response code 200 in initial request", 200, response.getStatusLine().getStatusCode());
Header head = response.getFirstHeader("ETag");
assertNotNull("We got no ETag in the response", head);
assertTrue("Not a valid ETag", head.getValue().startsWith("\"") && head.getValue().endsWith("\""));
String etag = head.getValue();
// If-None-Match tests
// we set a non matching ETag
get = getSelectMethod(method);
get.addHeader("If-None-Match", "\"xyz123456\"");
response = getClient().execute(get);
checkResponseBody(method, response);
assertEquals("If-None-Match: Got no response code 200 in response to non matching ETag", 200, response.getStatusLine().getStatusCode());
// now we set matching ETags
get = getSelectMethod(method);
get.addHeader("If-None-Match", "\"xyz1223\"");
get.addHeader("If-None-Match", "\"1231323423\", \"1211211\", " + etag);
response = getClient().execute(get);
checkResponseBody(method, response);
assertEquals("If-None-Match: Got no response 304 to matching ETag", 304, response.getStatusLine().getStatusCode());
// we now set the special star ETag
get = getSelectMethod(method);
get.addHeader("If-None-Match", "*");
response = getClient().execute(get);
checkResponseBody(method, response);
assertEquals("If-None-Match: Got no response 304 for star ETag", 304, response.getStatusLine().getStatusCode());
// If-Match tests
// we set a non matching ETag
get = getSelectMethod(method);
get.addHeader("If-Match", "\"xyz123456\"");
response = getClient().execute(get);
checkResponseBody(method, response);
assertEquals("If-Match: Got no response code 412 in response to non matching ETag", 412, response.getStatusLine().getStatusCode());
// now we set matching ETags
get = getSelectMethod(method);
get.addHeader("If-Match", "\"xyz1223\"");
get.addHeader("If-Match", "\"1231323423\", \"1211211\", " + etag);
response = getClient().execute(get);
checkResponseBody(method, response);
assertEquals("If-Match: Got no response 200 to matching ETag", 200, response.getStatusLine().getStatusCode());
// now we set the special star ETag
get = getSelectMethod(method);
get.addHeader("If-Match", "*");
response = getClient().execute(get);
checkResponseBody(method, response);
assertEquals("If-Match: Got no response 200 to star ETag", 200, response.getStatusLine().getStatusCode());
}
use of org.apache.http.client.methods.HttpRequestBase in project lucene-solr by apache.
the class CacheHeaderTestBase method getSelectMethod.
protected HttpRequestBase getSelectMethod(String method, String... params) throws URISyntaxException {
HttpSolrClient client = (HttpSolrClient) getSolrClient();
HttpRequestBase m = null;
ArrayList<BasicNameValuePair> qparams = new ArrayList<>();
if (params.length == 0) {
qparams.add(new BasicNameValuePair("q", "solr"));
qparams.add(new BasicNameValuePair("qt", "standard"));
}
for (int i = 0; i < params.length / 2; i++) {
qparams.add(new BasicNameValuePair(params[i * 2], params[i * 2 + 1]));
}
URI uri = URI.create(client.getBaseURL() + "/select?" + URLEncodedUtils.format(qparams, StandardCharsets.UTF_8));
if ("GET".equals(method)) {
m = new HttpGet(uri);
} else if ("HEAD".equals(method)) {
m = new HttpHead(uri);
} else if ("POST".equals(method)) {
m = new HttpPost(uri);
}
return m;
}
use of org.apache.http.client.methods.HttpRequestBase in project lucene-solr by apache.
the class JettyWebappTest method testAdminUI.
public void testAdminUI() throws Exception {
// Currently not an extensive test, but it does fire up the JSP pages and make
// sure they compile ok
String adminPath = "http://127.0.0.1:" + port + context + "/";
byte[] bytes = IOUtils.toByteArray(new URL(adminPath).openStream());
// real error will be an exception
assertNotNull(bytes);
HttpClient client = HttpClients.createDefault();
HttpRequestBase m = new HttpGet(adminPath);
HttpResponse response = client.execute(m, HttpClientUtil.createNewHttpClientRequestContext());
assertEquals(200, response.getStatusLine().getStatusCode());
Header header = response.getFirstHeader("X-Frame-Options");
assertEquals("DENY", header.getValue().toUpperCase(Locale.ROOT));
m.releaseConnection();
}
use of org.apache.http.client.methods.HttpRequestBase 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);
}
}
Aggregations