List of usage examples for org.apache.commons.httpclient HttpParser parseHeaders
public static Header[] parseHeaders(InputStream paramInputStream, String paramString) throws IOException, HttpException
From source file:com.mirth.connect.server.userutil.HTTPUtil.java
/** * Converts a block of HTTP header fields into a Map containing each header key and value. * /*w ww. j a v a 2s . c o m*/ * @param str * The block of HTTP header fields to convert. * @return The converted Map containing header key-value pairs. * @throws Exception */ public static Map<String, String> parseHeaders(String str) throws Exception { Map<String, String> headersMap = new HashMap<String, String>(); Header[] headers = HttpParser.parseHeaders(new ByteArrayInputStream(str.getBytes()), "UTF-8"); for (int i = 0; i < headers.length; i++) { headersMap.put(headers[i].getName(), headers[i].getValue()); } return headersMap; }
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 ww. ja v a 2s . 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.warc.WARCRecord.java
/** * Parse WARC Header Line and Named Fields. * @param in Stream to read.//w w w .j a v a 2s. c o m * @param identifier Identifier for the hosting Reader. * @param offset Absolute offset into Reader. * @param strict Whether to be loose parsing or not. * @return An ArchiveRecordHeader. * @throws IOException */ protected ArchiveRecordHeader parseHeaders(final InputStream in, final String identifier, final long offset, final boolean strict) throws IOException { final Map<Object, Object> m = new HashMap<Object, Object>(); m.put(ABSOLUTE_OFFSET_KEY, new Long(offset)); m.put(READER_IDENTIFIER_FIELD_KEY, identifier); long startPosition = -1; if (in instanceof RepositionableStream) { startPosition = ((RepositionableStream) in).position(); } String firstLine = new String(HttpParser.readLine(in, WARC_HEADER_ENCODING)); if (firstLine == null || firstLine.length() <= 0) { throw new IOException("Failed to read WARC_MAGIC"); } if (!firstLine.startsWith(WARC_MAGIC)) { throw new IOException("Failed to find WARC MAGIC: " + firstLine); } // Here we start reading off the inputstream but we're reading the // stream direct rather than going via WARCRecord#read. The latter will // keep count of bytes read, digest and fail properly if EOR too soon... // We don't want digesting while reading Headers. // Header[] h = HttpParser.parseHeaders(in, WARC_HEADER_ENCODING); for (int i = 0; i < h.length; i++) { m.put(h[i].getName(), h[i].getValue()); } int headerLength = -1; if (in instanceof RepositionableStream) { headerLength = (int) (((RepositionableStream) in).position() - startPosition); } final int contentOffset = headerLength; incrementPosition(contentOffset); return new ArchiveRecordHeader() { private Map<Object, Object> headers = m; private int contentBegin = contentOffset; public String getDate() { return (String) this.headers.get(HEADER_KEY_DATE); } public String getDigest() { return null; // TODO: perhaps return block-digest? // superclass def implies this is calculated ("only after // read in totality"), not pulled from header // return (String)this.headers.get(HEADER_KEY_CHECKSUM); } public String getReaderIdentifier() { return (String) this.headers.get(READER_IDENTIFIER_FIELD_KEY); } public Set getHeaderFieldKeys() { return this.headers.keySet(); } public Map getHeaderFields() { return this.headers; } public Object getHeaderValue(String key) { return this.headers.get(key); } public long getLength() { Object o = this.headers.get(CONTENT_LENGTH); if (o == null) { return -1; } long contentLength = (o instanceof Long) ? ((Long) o).longValue() : Long.parseLong((String) o); return contentLength + contentOffset; } public String getMimetype() { return (String) this.headers.get(CONTENT_TYPE); } public long getOffset() { Object o = this.headers.get(ABSOLUTE_OFFSET_KEY); if (o == null) { return -1; } return (o instanceof Long) ? ((Long) o).longValue() : Long.parseLong((String) o); } public String getRecordIdentifier() { return (String) this.headers.get(RECORD_IDENTIFIER_FIELD_KEY); } public String getUrl() { return (String) this.headers.get(HEADER_KEY_URI); } public String getVersion() { return (String) this.headers.get(VERSION_FIELD_KEY); } public int getContentBegin() { return this.contentBegin; } @Override public String toString() { return this.headers.toString(); } }; }
From source file:com.eviware.soapui.impl.wsdl.monitor.TcpMonWsdlMonitorMessageExchange.java
private void parseReponseData(byte[] capturedResponseData, IncomingWss responseWss) { responseContentLength = capturedResponseData.length; ByteArrayInputStream in = new ByteArrayInputStream(capturedResponseData); try {//from ww w .j a v a 2s . c o m String line = null; do { line = HttpParser.readLine(in, HTTP_ELEMENT_CHARSET); } while (line != null && line.length() == 0); if (line == null) { throw new Exception("Missing request status line"); } Header[] headers = HttpParser.parseHeaders(in, HTTP_ELEMENT_CHARSET); if (headers != null) { for (Header header : headers) { responseHeaders.put(header.getName(), header.getValue()); } } responseContentType = responseHeaders.get("Content-Type", ""); if (responseContentType != null && responseContentType.toUpperCase().startsWith("MULTIPART")) { StringToStringMap values = StringToStringMap.fromHttpHeader(responseContentType); responseMmSupport = new MultipartMessageSupport( new MonitorMessageExchangeDataSource("monitor response", in, responseContentType), values.get("start"), null, true, SoapUI.getSettings().getBoolean(WsdlSettings.PRETTY_PRINT_RESPONSE_MESSAGES)); responseContentType = responseMmSupport.getRootPart().getContentType(); } else { this.responseContent = XmlUtils.prettyPrintXml(Tools.readAll(in, 0).toString()); } processResponseWss(responseWss); } catch (Exception e) { try { in.close(); } catch (IOException e1) { e1.printStackTrace(); } } }
From source file:eu.scape_project.archiventory.hadoop.ArcRecordReader.java
private Header[] getHttpHeaders(ArchiveRecord nativeRecord) throws IOException { if (nativeRecord instanceof ARCRecord) { return ((ARCRecord) nativeRecord).getHttpHeaders(); } else if (nativeRecord instanceof WARCRecord) { WARCRecord warcRecord = (WARCRecord) nativeRecord; if (warcRecord.hasContentHeaders()) { Header[] headers = HttpParser.parseHeaders(nativeRecord, DEFAULT_ENCODING); return headers; }// w ww . j ava2s . com } return new Header[0]; }
From source file:com.eviware.soapui.impl.wsdl.monitor.TcpMonWsdlMonitorMessageExchange.java
private void parseRequestData(byte[] capturedRequestData, IncomingWss requestWss) { requestContentLength = capturedRequestData.length; ByteArrayInputStream in = new ByteArrayInputStream(capturedRequestData); try {//from w w w. ja v a2 s . com String line = null; do { line = HttpParser.readLine(in, HTTP_ELEMENT_CHARSET); } while (line != null && line.length() == 0); if (line == null) { throw new Exception("Missing request status line"); } Header[] headers = HttpParser.parseHeaders(in, HTTP_ELEMENT_CHARSET); if (headers != null) { for (Header header : headers) { requestHeaders.put(header.getName(), header.getValue()); } } requestContentType = requestHeaders.get("Content-Type", ""); if (requestContentType != null && requestContentType.toUpperCase().startsWith("MULTIPART")) { StringToStringMap values = StringToStringMap.fromHttpHeader(requestContentType); requestMmSupport = new MultipartMessageSupport( new MonitorMessageExchangeDataSource("monitor request", in, requestContentType), values.get("start"), null, true, SoapUI.getSettings().getBoolean(WsdlSettings.PRETTY_PRINT_RESPONSE_MESSAGES)); requestContentType = requestMmSupport.getRootPart().getContentType(); } else { this.requestContent = XmlUtils.prettyPrintXml(Tools.readAll(in, 0).toString()); } processRequestWss(requestWss); operation = findOperation(); } catch (Exception e) { try { in.close(); } catch (IOException e1) { e1.printStackTrace(); } } }
From source file:com.cyberway.issue.io.arc.ARCRecord.java
/** * Read http header if present. Technique borrowed from HttpClient HttpParse * class.//from ww w .ja va 2s. c om * * @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 a 2s . c om 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 v a 2 s. c om 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.mule.transport.http.functional.MockHttpServer.java
protected HttpRequest parseRequest(InputStream in, String encoding) { try {/*from ww w . j av a2 s. c o m*/ String line = HttpParser.readLine(in, encoding); RequestLine requestLine = RequestLine.parseLine(line); return new HttpRequest(requestLine, HttpParser.parseHeaders(in, encoding), in, encoding); } catch (Exception e) { throw new RuntimeException(e); } }