List of usage examples for org.apache.commons.httpclient StatusLine StatusLine
public StatusLine(String paramString) throws HttpException
From source file:com.tw.go.plugin.material.artifactrepository.yum.exec.HttpConnectionCheckerTest.java
@Test public void shouldFailCheckConnectionToTheRepoWhenHttpClientReturnsAUnSuccessfulReturnCode() throws IOException { HttpClient httpClient = mock(HttpClient.class); GetMethod getMethod = mock(GetMethod.class); when(checker.getHttpClient()).thenReturn(httpClient); when(checker.getGetMethod(Matchers.<String>any())).thenReturn(getMethod); when(httpClient.executeMethod(getMethod)).thenReturn(HttpStatus.SC_UNAUTHORIZED); when(getMethod.getStatusLine()).thenReturn(new StatusLine("HTTP/1.1 401 Unauthorized")); try {/*from w w w.j av a 2 s .c o m*/ checker.checkConnection("url", new Credentials(null, null)); fail("should fail"); } catch (Exception e) { assertThat(e.getMessage(), is("HTTP/1.1 401 Unauthorized")); } verify(httpClient).executeMethod(getMethod); verify(getMethod).getStatusLine(); }
From source file:com.jivesoftware.os.jive.utils.http.client.ApacheHttpClient31BackedHttpClientOAuthTest.java
private void mockResponse(byte[] body, int statusCode, String reason) throws IOException { final InputStream bodyInputStream = new ByteArrayInputStream(body); final StatusLine statusLine = new StatusLine("HTTP/1.1 " + statusCode + " " + reason + "\n"); when(httpClient.executeMethod(argThat(new ArgumentMatcher<HttpMethod>() { @Override/*from w w w .jav a 2 s . co m*/ public boolean matches(Object o) { if (o instanceof HttpMethodBase) { HttpMethodBase methodBase = (HttpMethodBase) o; try { Field responseStreamField = HttpMethodBase.class.getDeclaredField("responseStream"); responseStreamField.setAccessible(true); responseStreamField.set(methodBase, bodyInputStream); responseStreamField.setAccessible(false); Field statusLineField = HttpMethodBase.class.getDeclaredField("statusLine"); statusLineField.setAccessible(true); statusLineField.set(methodBase, statusLine); statusLineField.setAccessible(false); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return true; } return false; } }))).thenReturn(200); }
From source file:is.landsbokasafn.deduplicator.indexer.WarcFileIterator.java
protected static CrawlDataItem processResponse(WARCRecord record, ArchiveRecordHeader header) throws IOException { CrawlDataItem cdi = new CrawlDataItem(); cdi.setURL(header.getUrl());/* w w w . j a va 2 s. c o m*/ cdi.setContentDigest((String) header.getHeaderValue(WARCConstants.HEADER_KEY_PAYLOAD_DIGEST)); cdi.setRevisit(false); cdi.setTimestamp(header.getDate()); cdi.setWarcRecordId((String) header.getHeaderValue(WARCConstants.HEADER_KEY_ID)); // Process the HTTP header, if any byte[] statusBytes = HttpParser.readRawLine(record); int eolCharCount = getEolCharsCount(statusBytes); if (eolCharCount > 0) { String statusLine = EncodingUtil.getString(statusBytes, 0, statusBytes.length - eolCharCount, WARCConstants.DEFAULT_ENCODING); if ((statusLine != null) && StatusLine.startsWithHTTP(statusLine)) { StatusLine status = new StatusLine(statusLine); cdi.setStatusCode(status.getStatusCode()); Header[] headers = HttpParser.parseHeaders(record, WARCConstants.DEFAULT_ENCODING); for (Header h : headers) { if (h.getName().equalsIgnoreCase("Content-Type")) { cdi.setMimeType(h.getValue()); } else if (h.getName().equalsIgnoreCase("ETag")) { cdi.setEtag(h.getValue()); } } } } return cdi; }
From source file:com.cyberway.issue.io.arc.ARCRecord.java
/** * Read http header if present. Technique borrowed from HttpClient HttpParse * class./*from w ww . j a v a 2 s . co m*/ * * @return ByteArrayInputStream with the http header in it or null if no * http header. * @throws IOException */ private InputStream readHttpHeader() throws IOException { // If judged a record that doesn't have an http header, return // immediately. if (!getHeader().getUrl().startsWith("http") || getHeader().getLength() <= MIN_HTTP_HEADER_LENGTH) { return null; } byte[] statusBytes = HttpParser.readRawLine(getIn()); int eolCharCount = getEolCharsCount(statusBytes); if (eolCharCount <= 0) { throw new IOException("Failed to read http status where one was expected: " + ((statusBytes == null) ? "" : new String(statusBytes))); } String statusLine = EncodingUtil.getString(statusBytes, 0, statusBytes.length - eolCharCount, ARCConstants.DEFAULT_ENCODING); if ((statusLine == null) || !StatusLine.startsWithHTTP(statusLine)) { if (statusLine.startsWith("DELETED")) { // Some old ARCs have deleted records like following: // http://vireo.gatech.edu:80/ebt-bin/nph-dweb/dynaweb/SGI_Developer/SGITCL_PG/@Generic__BookTocView/11108%3Btd%3D2 130.207.168.42 19991010131803 text/html 29202 // DELETED_TIME=20000425001133_DELETER=Kurt_REASON=alexalist // (follows ~29K spaces) // For now, throw a RecoverableIOException so if iterating over // records, we keep going. TODO: Later make a legitimate // ARCRecord from the deleted record rather than throw // exception. throw new DeletedARCRecordIOException(statusLine); } else { throw new IOException("Failed parse of http status line."); } } this.httpStatus = new StatusLine(statusLine); // Save off all bytes read. Keep them as bytes rather than // convert to strings so we don't have to worry about encodings // though this should never be a problem doing http headers since // its all supposed to be ascii. ByteArrayOutputStream baos = new ByteArrayOutputStream(statusBytes.length + 4 * 1024); baos.write(statusBytes); // Now read rest of the header lines looking for the separation // between header and body. for (byte[] lineBytes = null; true;) { lineBytes = HttpParser.readRawLine(getIn()); eolCharCount = getEolCharsCount(lineBytes); if (eolCharCount <= 0) { throw new IOException( "Failed reading http headers: " + ((lineBytes != null) ? new String(lineBytes) : null)); } // Save the bytes read. baos.write(lineBytes); if ((lineBytes.length - eolCharCount) <= 0) { // We've finished reading the http header. break; } } byte[] headerBytes = baos.toByteArray(); // Save off where body starts. this.getMetaData().setContentBegin(headerBytes.length); ByteArrayInputStream bais = new ByteArrayInputStream(headerBytes); if (!bais.markSupported()) { throw new IOException("ByteArrayInputStream does not support mark"); } bais.mark(headerBytes.length); // Read the status line. Don't let it into the parseHeaders function. // It doesn't know what to do with it. bais.read(statusBytes, 0, statusBytes.length); this.httpHeaders = HttpParser.parseHeaders(bais, ARCConstants.DEFAULT_ENCODING); this.getMetaData().setStatusCode(Integer.toString(getStatusCode())); bais.reset(); return bais; }
From source file:dk.netarkivet.wayback.batch.copycode.NetarchiveSuiteWARCRecordToSearchResultAdapter.java
private CaptureSearchResult adaptWARCHTTPResponse(CaptureSearchResult result, WARCRecord rec) throws IOException { ArchiveRecordHeader header = rec.getHeader(); // need to parse the documents HTTP message and headers here: WARCReader // does not implement this... yet.. byte[] statusBytes = HttpParser.readRawLine(rec); int eolCharCount = getEolCharsCount(statusBytes); if (eolCharCount <= 0) { throw new RecoverableIOException("Failed to read http status where one " + " was expected: " + ((statusBytes == null) ? "(null)" : new String(statusBytes))); }/* w w w . j a v a2 s . c o m*/ String statusLine = EncodingUtil.getString(statusBytes, 0, statusBytes.length - eolCharCount, ARCConstants.DEFAULT_ENCODING); if ((statusLine == null) || !StatusLine.startsWithHTTP(statusLine)) { throw new RecoverableIOException("Failed parse of http status line."); } StatusLine status = new StatusLine(statusLine); result.setHttpCode(String.valueOf(status.getStatusCode())); Header[] headers = HttpParser.parseHeaders(rec, ARCConstants.DEFAULT_ENCODING); annotater.annotateHTTPContent(result, rec, headers, header.getMimetype()); return result; }
From source file:com.tasktop.c2c.server.ssh.server.commands.AbstractInteractiveProxyCommand.java
private void readHttpResponse(InputStream proxyInput) throws IOException, CommandException { String statusLineText = HttpParser.readLine(proxyInput, HTTP_ENTITY_CHARSET); StatusLine statusLine = new StatusLine(statusLineText); if (statusLine.getStatusCode() != HttpServletResponse.SC_OK) { String message = Integer.toString(statusLine.getStatusCode()); String reasonPhrase = statusLine.getReasonPhrase(); if (reasonPhrase != null && !reasonPhrase.isEmpty()) { message += ": " + reasonPhrase; }//w w w. j a va 2 s . c o m throw new CommandException(-1, message); } Header[] parsedHeaders = HttpParser.parseHeaders(proxyInput, HTTP_ENTITY_CHARSET); HeaderGroup headerGroup = new HeaderGroup(); headerGroup.setHeaders(parsedHeaders); Header transferEncoding = headerGroup.getFirstHeader("Transfer-Encoding"); if (transferEncoding == null || !transferEncoding.getValue().equals("chunked")) { throw new IOException("Expected Transfer-Encoding of \"chunked\" but received " + transferEncoding); } Header contentType = headerGroup.getFirstHeader("Content-Type"); if (contentType == null || !contentType.getValue().equals(MIME_TYPE_APPLICATION_OCTET_STREAM)) { throw new IOException("Unexpected Content-Type " + contentType); } }
From source file:org.apache.ode.axis2.httpbinding.HttpClientHelper.java
/** * Parse and convert a HTTP status line into an aml element. * * @param statusLine/*from w w w. j a va2 s.c om*/ * @return * @throws HttpException * @see #statusLineToElement(org.w3c.dom.Document, org.apache.commons.httpclient.StatusLine) */ public static Element statusLineToElement(String statusLine) throws HttpException { return statusLineToElement(new StatusLine(statusLine)); }
From source file:org.archive.io.arc.ARCRecord.java
/** * Read http header if present. Technique borrowed from HttpClient HttpParse * class. set errors when found.// ww w .ja v a2 s. co m * * @return ByteArrayInputStream with the http header in it or null if no * http header. * @throws IOException */ private InputStream readHttpHeader() throws IOException { // this can be helpful when simply iterating over records, // looking for problems. Logger logger = Logger.getLogger(this.getClass().getName()); ArchiveRecordHeader h = this.getHeader(); // If judged a record that doesn't have an http header, return // immediately. String url = getHeader().getUrl(); if (!url.startsWith("http") || getHeader().getLength() <= MIN_HTTP_HEADER_LENGTH) { return null; } String statusLine; byte[] statusBytes; int eolCharCount = 0; int errOffset = 0; // Read status line, skipping any errant http headers found before it // This allows a larger number of 'corrupt' arcs -- where headers were accidentally // inserted before the status line to be readable while (true) { statusBytes = LaxHttpParser.readRawLine(getIn()); eolCharCount = getEolCharsCount(statusBytes); if (eolCharCount <= 0) { throw new RecoverableIOException("Failed to read http status where one was expected: " + ((statusBytes == null) ? "" : new String(statusBytes))); } statusLine = EncodingUtil.getString(statusBytes, 0, statusBytes.length - eolCharCount, ARCConstants.DEFAULT_ENCODING); // If a null or DELETED break immediately if ((statusLine == null) || statusLine.startsWith("DELETED")) { break; } // If it's actually the status line, break, otherwise continue skipping any // previous header values if (!statusLine.contains(":") && StatusLine.startsWithHTTP(statusLine)) { break; } // Add bytes read to error "offset" to add to position errOffset += statusBytes.length; } if (errOffset > 0) { this.incrementPosition(errOffset); } if ((statusLine == null) || !StatusLine.startsWithHTTP(statusLine)) { if (statusLine.startsWith("DELETED")) { // Some old ARCs have deleted records like following: // http://vireo.gatech.edu:80/ebt-bin/nph-dweb/dynaweb/SGI_Developer/SGITCL_PG/@Generic__BookTocView/11108%3Btd%3D2 130.207.168.42 19991010131803 text/html 29202 // DELETED_TIME=20000425001133_DELETER=Kurt_REASON=alexalist // (follows ~29K spaces) // For now, throw a RecoverableIOException so if iterating over // records, we keep going. TODO: Later make a legitimate // ARCRecord from the deleted record rather than throw // exception. throw new DeletedARCRecordIOException(statusLine); } else { this.errors.add(ArcRecordErrors.HTTP_STATUS_LINE_INVALID); } } try { this.httpStatus = new StatusLine(statusLine); } catch (IOException e) { logger.warning(e.getMessage() + " at offset: " + h.getOffset()); this.errors.add(ArcRecordErrors.HTTP_STATUS_LINE_EXCEPTION); } // Save off all bytes read. Keep them as bytes rather than // convert to strings so we don't have to worry about encodings // though this should never be a problem doing http headers since // its all supposed to be ascii. ByteArrayOutputStream baos = new ByteArrayOutputStream(statusBytes.length + 4 * 1024); baos.write(statusBytes); // Now read rest of the header lines looking for the separation // between header and body. for (byte[] lineBytes = null; true;) { lineBytes = LaxHttpParser.readRawLine(getIn()); eolCharCount = getEolCharsCount(lineBytes); if (eolCharCount <= 0) { if (getIn().available() == 0) { httpHeaderBytesRead += statusBytes.length; logger.warning("HTTP header truncated at offset: " + h.getOffset()); this.errors.add(ArcRecordErrors.HTTP_HEADER_TRUNCATED); this.setEor(true); break; } else { throw new IOException( "Failed reading http headers: " + ((lineBytes != null) ? new String(lineBytes) : null)); } } else { httpHeaderBytesRead += lineBytes.length; } // Save the bytes read. baos.write(lineBytes); if ((lineBytes.length - eolCharCount) <= 0) { // We've finished reading the http header. break; } } byte[] headerBytes = baos.toByteArray(); // Save off where body starts. this.getMetaData().setContentBegin(headerBytes.length); ByteArrayInputStream bais = new ByteArrayInputStream(headerBytes); if (!bais.markSupported()) { throw new IOException("ByteArrayInputStream does not support mark"); } bais.mark(headerBytes.length); // Read the status line. Don't let it into the parseHeaders function. // It doesn't know what to do with it. bais.read(statusBytes, 0, statusBytes.length); this.httpHeaders = LaxHttpParser.parseHeaders(bais, ARCConstants.DEFAULT_ENCODING); this.getMetaData().setStatusCode(Integer.toString(getStatusCode())); bais.reset(); return bais; }
From source file:org.archive.io.HeaderedArchiveRecord.java
/** * Read header if present. Technique borrowed from HttpClient HttpParse * class. Using http parser code for now. Later move to more generic header * parsing code if there proves a need./*from ww w.j a v a 2 s . c o m*/ * * @return ByteArrayInputStream with the http header in it or null if no * http header. * @throws IOException */ private InputStream readContentHeaders() throws IOException { // If judged a record that doesn't have an http header, return // immediately. if (!hasContentHeaders()) { return null; } byte[] statusBytes = LaxHttpParser.readRawLine(getIn()); int eolCharCount = getEolCharsCount(statusBytes); if (eolCharCount <= 0) { throw new IOException( "Failed to read raw lie where one " + " was expected: " + new String(statusBytes)); } String statusLine = EncodingUtil.getString(statusBytes, 0, statusBytes.length - eolCharCount, ARCConstants.DEFAULT_ENCODING); if (statusLine == null) { throw new NullPointerException("Expected status line is null"); } // TODO: Tighten up this test. boolean isHttpResponse = StatusLine.startsWithHTTP(statusLine); boolean isHttpRequest = false; if (!isHttpResponse) { isHttpRequest = statusLine.toUpperCase().startsWith("GET") || !statusLine.toUpperCase().startsWith("POST"); } if (!isHttpResponse && !isHttpRequest) { throw new UnexpectedStartLineIOException("Failed parse of " + "status line: " + statusLine); } this.statusCode = isHttpResponse ? (new StatusLine(statusLine)).getStatusCode() : -1; // Save off all bytes read. Keep them as bytes rather than // convert to strings so we don't have to worry about encodings // though this should never be a problem doing http headers since // its all supposed to be ascii. ByteArrayOutputStream baos = new ByteArrayOutputStream(statusBytes.length + 4 * 1024); baos.write(statusBytes); // Now read rest of the header lines looking for the separation // between header and body. for (byte[] lineBytes = null; true;) { lineBytes = LaxHttpParser.readRawLine(getIn()); eolCharCount = getEolCharsCount(lineBytes); if (eolCharCount <= 0) { throw new IOException( "Failed reading headers: " + ((lineBytes != null) ? new String(lineBytes) : null)); } // Save the bytes read. baos.write(lineBytes); if ((lineBytes.length - eolCharCount) <= 0) { // We've finished reading the http header. break; } } byte[] headerBytes = baos.toByteArray(); // Save off where content body, post content headers, starts. this.contentHeadersLength = headerBytes.length; ByteArrayInputStream bais = new ByteArrayInputStream(headerBytes); if (!bais.markSupported()) { throw new IOException("ByteArrayInputStream does not support mark"); } bais.mark(headerBytes.length); // Read the status line. Don't let it into the parseHeaders function. // It doesn't know what to do with it. bais.read(statusBytes, 0, statusBytes.length); this.contentHeaders = LaxHttpParser.parseHeaders(bais, ARCConstants.DEFAULT_ENCODING); bais.reset(); return bais; }
From source file:org.archive.jbs.arc.ArchiveRecordProxy.java
/** * Construct an WARCRecord proxy. Read at most sizeLimit * bytes from the record body./*from w w w . j av a 2 s.com*/ */ public ArchiveRecordProxy(WARCRecord warc, int sizeLimit) throws IOException { ArchiveRecordHeader header = warc.getHeader(); this.warcRecordType = (String) header.getHeaderValue(WARCConstants.HEADER_KEY_TYPE); this.warcContentType = (String) header.getHeaderValue(WARCConstants.CONTENT_TYPE); this.url = header.getUrl(); this.digest = (String) header.getHeaderValue(WARCConstants.HEADER_KEY_PAYLOAD_DIGEST); // Convert to familiar YYYYMMDDHHMMSS format String warcDate = (String) header.getHeaderValue(WARCConstants.HEADER_KEY_DATE); this.date = new StringBuilder().append(warcDate, 0, 4).append(warcDate, 5, 7).append(warcDate, 8, 10) .append(warcDate, 11, 13).append(warcDate, 14, 16).append(warcDate, 17, 19).toString(); // Check if HTTP (not dns or the like) and then read the HTTP // headers so that the file position is at the response body if (WARCConstants.HTTP_RESPONSE_MIMETYPE.equals(this.warcContentType)) { // Sometimes an HTTP response will have whitespace (such as // blank lines) before the actual HTTP status line like: // [blank] // HTTP/1.0 200 OK // so we have to gobble them up. String line; while ((line = HttpParser.readLine(warc, "utf-8")) != null) { line = line.trim(); // If an empty line, or an invalid HTTP-status line: skip it! if (line.length() == 0) continue; if (!line.startsWith("HTTP")) continue; try { // Now get on with parsing the status line. StatusLine statusLine = new StatusLine(line); this.code = Integer.toString(statusLine.getStatusCode()); break; } catch (HttpException e) { // The line started with "HTTP", but was not a full, // valid HTTP-Status line. Assume that we won't see // one, so break out of the loop. // But first, set the HTTP code to a value indicating // there was no valid HTTP code. this.code = ""; break; } } // Skip over the HTTP headers, we just want the body of the HTTP response. skipHttpHeaders(warc); // The length of the HTTP response body is equal to the number // of bytes remaining in the WARC record. this.length = header.getLength() - warc.getPosition(); this.body = readBytes(warc, this.length, sizeLimit); } else if (WARCConstants.WARCRecordType.resource.toString().equals(this.warcRecordType) && // We check for "ftp://" here because we don't want to waste the time to copy the // bytes for resource record types we don't care about. If we want to pass along // all resource records, simply remove this check. this.url.startsWith("ftp://")) { // HACK: We set a bogus HTTP status code 200 here because // later in the indexing workflow, non-200 codes are // filtered out so that we don't index 404 pages and // such. this.code = "200"; // The length of the FTP response body is equal to the number // of bytes remaining in the WARC record. this.length = header.getLength() - warc.getPosition(); this.body = readBytes(warc, this.length, sizeLimit); } }