use of org.apache.http.client.methods.HttpRequestBase in project newsrob by marianokamp.
the class GRAnsweredBadRequestException method getStreamIDsFromGR.
/**
* @param xt
* can be null
*/
protected long[] getStreamIDsFromGR(NewsRobHttpClient httpClient, final List<String> tags, String xt, int max) throws IOException, SAXException, ParserConfigurationException, GRTokenExpiredException, GRAnsweredBadRequestException {
if (max == 0)
return new long[0];
final String tagsLabel = String.valueOf(tags);
Timing t = new Timing("EntriesRetriever.getStreamIDsFromGR(" + tagsLabel + ") (-" + xt + ")", context);
int currentCapacity = getEntryManager().getArticleCount();
String url = getGoogleHost() + "/reader/api/0/stream/items/ids";
url += "?s=" + tags.remove(0);
for (String tag : tags) url += "&s=" + tag;
if (xt != null)
url += "&xt=" + xt;
url += "&n=" + max;
try {
HttpRequestBase req = createGRRequest(httpClient, url);
HttpResponse response = executeGRRequest(httpClient, req, true);
throwExceptionWhenNotStatusOK(response);
final List<Long> unreadIds = new ArrayList<Long>(currentCapacity * 80 / 100);
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
SAXParser parser = saxParserFactory.newSAXParser();
DefaultHandler handler = new SimpleStringExtractorHandler() {
String currentName;
boolean validResponse;
@Override
public final void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {
super.startElement(uri, localName, name, attributes);
currentName = attributes.getValue("name");
if (!validResponse && "/object".equals(getFullyQualifiedPathName()))
validResponse = true;
}
@Override
public final void receivedString(String localTagName, String fqn, String value) {
if ("number".equals(localTagName) && "id".equals(currentName))
unreadIds.add(Long.parseLong(value));
}
@Override
public void endDocument() throws SAXException {
super.endDocument();
if (!validResponse)
throw new RuntimeException("Google Reader response was invalid. Proxy issue?");
}
};
InputStream is = NewsRobHttpClient.getUngzippedContent(response.getEntity(), context);
parser.parse(is, handler);
if (NewsRob.isDebuggingEnabled(context))
PL.log(TAG + ": GR returned number of articles(" + tagsLabel + ") (-" + xt + ")=" + unreadIds.size(), context);
long[] rv = new long[unreadIds.size()];
int idx = 0;
for (Long unreadId : unreadIds) rv[idx++] = unreadId;
return rv;
} finally {
t.stop();
}
}
use of org.apache.http.client.methods.HttpRequestBase in project newsrob by marianokamp.
the class GRAnsweredBadRequestException method requestArticlesFromGoogleReader.
private void requestArticlesFromGoogleReader(Job job, FetchContext fetchCtx, NewsRobHttpClient httpClient, String state, int n, String urlPostfix) throws IOException, FactoryConfigurationError, ParserConfigurationException, SAXException, GRTokenExpiredException, GRAnsweredBadRequestException {
if (n == 0)
return;
String url = getGoogleHost() + "/reader/atom/" + state + "?n=" + n + "&r=n" + urlPostfix;
HttpRequestBase req = createGRRequest(httpClient, url);
HttpResponse response = executeGRRequest(httpClient, req, true);
throwExceptionWhenNotStatusOK(response);
processReadingList(job, fetchCtx, response);
Log.d(TAG, "totalFetchedArticleCount=" + fetchCtx.countFetchedEntries);
Log.d(TAG, "totalSeenArticleCount=" + fetchCtx.countFetchedEntries);
}
use of org.apache.http.client.methods.HttpRequestBase in project newsrob by marianokamp.
the class GRAnsweredBadRequestException method updateSubscriptionList.
void updateSubscriptionList(final EntryManager entryManager, final Job job) throws IOException, ParserConfigurationException, SAXException, GRTokenExpiredException {
if (job.isCancelled())
return;
if (entryManager.getLastSyncedSubscriptions() != -1l && System.currentTimeMillis() < entryManager.getLastSyncedSubscriptions() + ONE_DAY_IN_MS) {
PL.log("Not updating subscription list this time.", context);
return;
}
PL.log("Updating subscription list.", context);
Timing t = new Timing("UpdateSubscriptionList", context);
final NewsRobHttpClient httpClient = NewsRobHttpClient.newInstance(false, context);
try {
final String url = getGoogleHost() + "/reader/api/0/subscription/list";
HttpRequestBase req = createGRRequest(httpClient, url);
HttpResponse response;
try {
response = executeGRRequest(httpClient, req, true);
} catch (GRAnsweredBadRequestException e) {
throw new IOException("GR: Bad Request.");
}
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
SAXParser parser = saxParserFactory.newSAXParser();
final Map<String, String> remoteTitlesAndIds = new HashMap<String, String>(107);
DefaultHandler handler = new SimpleStringExtractorHandler() {
private String currentFeedAtomId;
private String currentString;
@Override
public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {
if (job.isCancelled())
throw new UpdateSubscriptionsCancelledException();
super.startElement(uri, localName, name, attributes);
String fqn = getFullyQualifiedPathName();
if ("/object/list/object".equals(fqn)) {
currentFeedAtomId = null;
} else if ("/object/list/object/string".equals(fqn)) {
currentString = attributes.getValue("name");
}
}
@Override
public void receivedString(String localName, String fqn, String s) {
if (!"/object/list/object/string".equals(fqn))
return;
if ("id".equals(currentString))
currentFeedAtomId = TAG_GR + s;
else if ("title".equals(currentString)) {
if (currentFeedAtomId != null)
remoteTitlesAndIds.put(currentFeedAtomId, s);
// entryManager.updateFeedName(currentFeedAtomId, s);
}
}
};
parser.parse(NewsRobHttpClient.getUngzippedContent(response.getEntity(), context), handler);
if (NewsRob.isDebuggingEnabled(context))
PL.log("Got subscription list with " + remoteTitlesAndIds.size() + " feeds.", context);
if (job.isCancelled())
return;
entryManager.updateFeedNames(remoteTitlesAndIds);
} finally {
httpClient.close();
t.stop();
}
entryManager.updateLastSyncedSubscriptions(System.currentTimeMillis());
}
use of org.apache.http.client.methods.HttpRequestBase in project jmeter by apache.
the class HTTPHC4Impl method sample.
@Override
protected HTTPSampleResult sample(URL url, String method, boolean areFollowingRedirect, int frameDepth) {
if (log.isDebugEnabled()) {
log.debug("Start : sample {} method {} followingRedirect {} depth {}", url, method, areFollowingRedirect, frameDepth);
}
HTTPSampleResult res = createSampleResult(url, method);
CloseableHttpClient httpClient = setupClient(url);
HttpRequestBase httpRequest = null;
try {
URI uri = url.toURI();
if (method.equals(HTTPConstants.POST)) {
httpRequest = new HttpPost(uri);
} else if (method.equals(HTTPConstants.GET)) {
// otherwise we use HttpGetWithEntity
if (!areFollowingRedirect && ((!hasArguments() && getSendFileAsPostBody()) || getSendParameterValuesAsPostBody())) {
httpRequest = new HttpGetWithEntity(uri);
} else {
httpRequest = new HttpGet(uri);
}
} else if (method.equals(HTTPConstants.PUT)) {
httpRequest = new HttpPut(uri);
} else if (method.equals(HTTPConstants.HEAD)) {
httpRequest = new HttpHead(uri);
} else if (method.equals(HTTPConstants.TRACE)) {
httpRequest = new HttpTrace(uri);
} else if (method.equals(HTTPConstants.OPTIONS)) {
httpRequest = new HttpOptions(uri);
} else if (method.equals(HTTPConstants.DELETE)) {
httpRequest = new HttpDelete(uri);
} else if (method.equals(HTTPConstants.PATCH)) {
httpRequest = new HttpPatch(uri);
} else if (HttpWebdav.isWebdavMethod(method)) {
httpRequest = new HttpWebdav(method, uri);
} else {
throw new IllegalArgumentException("Unexpected method: '" + method + "'");
}
// can throw IOException
setupRequest(url, httpRequest, res);
} catch (Exception e) {
res.sampleStart();
res.sampleEnd();
errorResult(e, res);
return res;
}
HttpContext localContext = new BasicHttpContext();
setupClientContextBeforeSample(localContext);
res.sampleStart();
final CacheManager cacheManager = getCacheManager();
if (cacheManager != null && HTTPConstants.GET.equalsIgnoreCase(method) && cacheManager.inCache(url)) {
return updateSampleResultForResourceInCache(res);
}
CloseableHttpResponse httpResponse = null;
try {
currentRequest = httpRequest;
handleMethod(method, res, httpRequest, localContext);
// store the SampleResult in LocalContext to compute connect time
localContext.setAttribute(SAMPLER_RESULT_TOKEN, res);
// perform the sample
httpResponse = executeRequest(httpClient, httpRequest, localContext, url);
// Needs to be done after execute to pick up all the headers
final HttpRequest request = (HttpRequest) localContext.getAttribute(HttpCoreContext.HTTP_REQUEST);
extractClientContextAfterSample(localContext);
// We've finished with the request, so we can add the LocalAddress to it for display
final InetAddress localAddr = (InetAddress) httpRequest.getParams().getParameter(ConnRoutePNames.LOCAL_ADDRESS);
if (localAddr != null) {
request.addHeader(HEADER_LOCAL_ADDRESS, localAddr.toString());
}
res.setRequestHeaders(getConnectionHeaders(request));
Header contentType = httpResponse.getLastHeader(HTTPConstants.HEADER_CONTENT_TYPE);
if (contentType != null) {
String ct = contentType.getValue();
res.setContentType(ct);
res.setEncodingAndType(ct);
}
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
res.setResponseData(readResponse(res, entity.getContent(), entity.getContentLength()));
}
// Done with the sampling proper.
res.sampleEnd();
currentRequest = null;
// Now collect the results into the HTTPSampleResult:
StatusLine statusLine = httpResponse.getStatusLine();
int statusCode = statusLine.getStatusCode();
res.setResponseCode(Integer.toString(statusCode));
res.setResponseMessage(statusLine.getReasonPhrase());
res.setSuccessful(isSuccessCode(statusCode));
res.setResponseHeaders(getResponseHeaders(httpResponse, localContext));
if (res.isRedirect()) {
final Header headerLocation = httpResponse.getLastHeader(HTTPConstants.HEADER_LOCATION);
if (headerLocation == null) {
// HTTP protocol violation, but avoids NPE
throw new IllegalArgumentException("Missing location header in redirect for " + httpRequest.getRequestLine());
}
String redirectLocation = headerLocation.getValue();
res.setRedirectLocation(redirectLocation);
}
// record some sizes to allow HTTPSampleResult.getBytes() with different options
HttpConnectionMetrics metrics = (HttpConnectionMetrics) localContext.getAttribute(CONTEXT_METRICS);
long headerBytes = // condensed length (without \r)
(long) res.getResponseHeaders().length() + // Add \r for each header
(long) httpResponse.getAllHeaders().length + // Add \r for initial header
1L + // final \r\n before data
2L;
long totalBytes = metrics.getReceivedBytesCount();
res.setHeadersSize((int) headerBytes);
res.setBodySize(totalBytes - headerBytes);
res.setSentBytes(metrics.getSentBytesCount());
if (log.isDebugEnabled()) {
log.debug("ResponseHeadersSize={} Content-Length={} Total={}", res.getHeadersSize(), res.getBodySizeAsLong(), (res.getHeadersSize() + res.getBodySizeAsLong()));
}
// If we redirected automatically, the URL may have changed
if (getAutoRedirects()) {
HttpUriRequest req = (HttpUriRequest) localContext.getAttribute(HttpCoreContext.HTTP_REQUEST);
HttpHost target = (HttpHost) localContext.getAttribute(HttpCoreContext.HTTP_TARGET_HOST);
URI redirectURI = req.getURI();
if (redirectURI.isAbsolute()) {
res.setURL(redirectURI.toURL());
} else {
res.setURL(new URL(new URL(target.toURI()), redirectURI.toString()));
}
}
// Store any cookies received in the cookie manager:
saveConnectionCookies(httpResponse, res.getURL(), getCookieManager());
// Save cache information
if (cacheManager != null) {
cacheManager.saveDetails(httpResponse, res);
}
// Follow redirects and download page resources if appropriate:
res = resultProcessing(areFollowingRedirect, frameDepth, res);
} catch (IOException e) {
log.debug("IOException", e);
if (res.getEndTime() == 0) {
res.sampleEnd();
}
// pick up headers if failed to execute the request
if (res.getRequestHeaders() != null) {
log.debug("Overwriting request old headers: {}", res.getRequestHeaders());
}
res.setRequestHeaders(getConnectionHeaders((HttpRequest) localContext.getAttribute(HttpCoreContext.HTTP_REQUEST)));
errorResult(e, res);
return res;
} catch (RuntimeException e) {
log.debug("RuntimeException", e);
if (res.getEndTime() == 0) {
res.sampleEnd();
}
errorResult(e, res);
return res;
} finally {
JOrphanUtils.closeQuietly(httpResponse);
currentRequest = null;
JMeterContextService.getContext().getSamplerContext().remove(HTTPCLIENT_TOKEN);
}
return res;
}
use of org.apache.http.client.methods.HttpRequestBase in project lucene-solr by apache.
the class HttpSolrClient method request.
public NamedList<Object> request(final SolrRequest request, final ResponseParser processor, String collection) throws SolrServerException, IOException {
HttpRequestBase method = createMethod(request, collection);
setBasicAuthHeader(request, method);
return executeMethod(method, processor);
}
Aggregations