List of usage examples for org.apache.commons.httpclient HttpMethodBase getResponseHeaders
@Override
public Header[] getResponseHeaders()
From source file:cz.muni.fi.pa165.creatures.rest.client.services.utils.CRUDServiceHelper.java
/** * Helper methods which executes an HTTP {@code method} and writes the * output to the standard output (e.g. console). Return codes of the HTTP * responses are present so we can verify what happened at the server side. * * @param method method to send to the server side *///from ww w .java 2 s .c o m public static void send(HttpMethodBase method) { HttpClient httpClient = new HttpClient(); try { int result = httpClient.executeMethod(method); System.out.println(RESPONSE_STATUS_CODE + result); ResponseCode response = ResponseCode.fromInt(result); if (response != ResponseCode.NOT_FOUND && response != ResponseCode.SERVER_ERROR && response != ResponseCode.FORBIDDEN) { System.out.println(RESPONSE_HEADER); Header[] headers = method.getResponseHeaders(); for (Header h : headers) { System.out.println(h.toString()); } InputStreamReader isr = new InputStreamReader(method.getResponseBodyAsStream()); BufferedReader br = new BufferedReader(isr); String line; while ((line = br.readLine()) != null) { System.out.println(line); } } } catch (ConnectException ex) { logger.log(Level.WARNING, CONNECTION_EXCEPTION_MSG); } catch (IOException ex) { logger.log(Level.INFO, ex.getMessage()); } finally { method.releaseConnection(); } }
From source file:com.sittinglittleduck.DirBuster.Worker.java
private String getHeadersAsString(HttpMethodBase httpMethod) { Header[] headers = httpMethod.getResponseHeaders(); StringBuilder builder = new StringBuilder(20 * (headers.length + 1)); builder.append(httpMethod.getStatusLine()); builder.append("\r\n"); for (Header header : headers) { builder.append(header.getName()).append(": ").append(header.getValue()); builder.append("\r\n"); }// w ww . j a va2 s .c o m return builder.append("\r\n").toString(); }
From source file:jeeves.utils.XmlRequest.java
private String getReceivedData(HttpMethodBase httpMethod, byte[] response) { StringBuilder receivedData = new StringBuilder(); try {/*w w w . j a v a 2 s . co m*/ //--- if there is a connection error (the server is unreachable) this //--- call causes a NullPointerEx receivedData.append(httpMethod.getStatusText()).append("\r\r"); for (Header h : httpMethod.getResponseHeaders()) { receivedData.append(h); } receivedData.append("\r\n"); if (response != null) { receivedData.append(new String(response, "UTF8")); } } catch (Exception e) { receivedData.setLength(0); } return receivedData.toString(); }
From source file:com.esri.gpt.framework.http.HttpClientRequest.java
/** * Determines basic information associted with an HTTP response. * <br/>For example: response status message, Content-Type, Content-Length * @param method the HttpMethod that was executed *//*from w w w.j av a 2 s.co m*/ private void determineResponseInfo(HttpMethodBase method) { this.getResponseInfo().setResponseMessage(method.getStatusText()); this.getResponseInfo().setResponseHeaders(method.getResponseHeaders()); Header contentTypeHeader = method.getResponseHeader("Content-Type"); if (contentTypeHeader != null) { HeaderElement values[] = contentTypeHeader.getElements(); // Expect only one header element to be there, no more, no less if (values.length == 1) { this.getResponseInfo().setContentType(values[0].getName()); NameValuePair param = values[0].getParameterByName("charset"); if (param != null) { // If invalid, an UnsupportedEncondingException will result this.getResponseInfo().setContentEncoding(param.getValue()); } } } this.getResponseInfo().setContentLength(method.getResponseContentLength()); }
From source file:com.progress.codeshare.esbservice.http.HTTPService.java
public void service(final XQServiceContext ctx) throws XQServiceException { try {//from w w w . j av a 2 s . co m final XQMessageFactory factory = ctx.getMessageFactory(); final XQParameters params = ctx.getParameters(); final int messagePart = params.getIntParameter(PARAM_MESSAGE_PART, XQConstants.PARAM_STRING); final String method = params.getParameter(PARAM_METHOD, XQConstants.PARAM_STRING); final String uri = params.getParameter(PARAM_URI, XQConstants.PARAM_STRING); while (ctx.hasNextIncoming()) { final XQEnvelope env = ctx.getNextIncoming(); final XQMessage origMsg = env.getMessage(); final XQMessage newMsg = factory.createMessage(); final HttpClient client = new HttpClient(); final Iterator headerIterator = origMsg.getHeaderNames(); if (METHOD_DELETE.equals(method)) { final HttpMethodBase req = new DeleteMethod(uri); /* * Copy all XQ headers and extract HTTP headers and * parameters */ while (headerIterator.hasNext()) { final String header = (String) headerIterator.next(); newMsg.setHeaderValue(header, origMsg.getHeaderValue(header)); final Matcher matcher = PATTERN_HEADER.matcher(header); if (matcher.find()) req.addRequestHeader(matcher.group(1), (String) origMsg.getHeaderValue(matcher.group())); } client.executeMethod(req); /* Transform all HTTP to XQ headers */ final Header[] headers = req.getResponseHeaders(); for (int i = 0; i < headers.length; i++) newMsg.setHeaderValue(PREFIX_HEADER + headers[i].getName(), headers[i].getValue()); final XQPart newPart = newMsg.createPart(); newPart.setContentId("Result"); newPart.setContent(new String(req.getResponseBody()), req.getResponseHeader("Content-Type").getValue()); newMsg.addPart(newPart); } else if (METHOD_GET.equals(method)) { final HttpMethodBase req = new GetMethod(); final List paramList = new ArrayList(); /* * Copy all XQ headers and extract HTTP headers and * parameters */ while (headerIterator.hasNext()) { final String header = (String) headerIterator.next(); newMsg.setHeaderValue(header, origMsg.getHeaderValue(header)); final Matcher headerMatcher = PATTERN_HEADER.matcher(header); if (headerMatcher.find()) { req.addRequestHeader(headerMatcher.group(1), (String) origMsg.getHeaderValue(headerMatcher.group())); continue; } final Matcher paramMatcher = PATTERN_PARAM.matcher(header); if (paramMatcher.find()) paramList.add(new NameValuePair(paramMatcher.group(1), (String) origMsg.getHeaderValue(paramMatcher.group()))); } req.setQueryString((NameValuePair[]) paramList.toArray(new NameValuePair[] {})); client.executeMethod(req); /* Transform all HTTP to XQ headers */ final Header[] headers = req.getResponseHeaders(); for (int i = 0; i < headers.length; i++) newMsg.setHeaderValue(PREFIX_HEADER + headers[i].getName(), headers[i].getValue()); final XQPart newPart = newMsg.createPart(); newPart.setContentId("Result"); newPart.setContent(new String(req.getResponseBody()), req.getResponseHeader("Content-Type").getValue()); newMsg.addPart(newPart); } else if (METHOD_POST.equals(method)) { final PostMethod req = new PostMethod(uri); /* * Copy all XQ headers and extract HTTP headers and * parameters */ while (headerIterator.hasNext()) { final String header = (String) headerIterator.next(); newMsg.setHeaderValue(header, origMsg.getHeaderValue(header)); final Matcher headerMatcher = PATTERN_HEADER.matcher(header); if (headerMatcher.find()) { req.addRequestHeader(headerMatcher.group(1), (String) origMsg.getHeaderValue(headerMatcher.group())); continue; } final Matcher paramMatcher = PATTERN_PARAM.matcher(header); if (paramMatcher.find()) req.addParameter(new NameValuePair(paramMatcher.group(1), (String) origMsg.getHeaderValue(paramMatcher.group()))); } final XQPart origPart = origMsg.getPart(messagePart); req.setRequestEntity(new StringRequestEntity((String) origPart.getContent(), origPart.getContentType(), null)); client.executeMethod(req); /* Transform all HTTP to XQ headers */ final Header[] headers = req.getResponseHeaders(); for (int i = 0; i < headers.length; i++) newMsg.setHeaderValue(PREFIX_HEADER + headers[i].getName(), headers[i].getValue()); final XQPart newPart = newMsg.createPart(); newPart.setContentId("Result"); newPart.setContent(new String(req.getResponseBody()), req.getResponseHeader("Content-Type").getValue()); newMsg.addPart(newPart); } else if (METHOD_PUT.equals(method)) { final EntityEnclosingMethod req = new PutMethod(uri); /* Copy all XQ headers and extract HTTP headers */ while (headerIterator.hasNext()) { final String header = (String) headerIterator.next(); newMsg.setHeaderValue(header, origMsg.getHeaderValue(header)); final Matcher matcher = PATTERN_HEADER.matcher(header); if (matcher.find()) req.addRequestHeader(matcher.group(1), (String) origMsg.getHeaderValue(matcher.group())); } final XQPart origPart = origMsg.getPart(messagePart); req.setRequestEntity(new StringRequestEntity((String) origPart.getContent(), origPart.getContentType(), null)); client.executeMethod(req); /* Transform all HTTP to XQ headers */ final Header[] headers = req.getResponseHeaders(); for (int i = 0; i < headers.length; i++) newMsg.setHeaderValue(PREFIX_HEADER + headers[i].getName(), headers[i].getValue()); final XQPart newPart = newMsg.createPart(); newPart.setContentId("Result"); newPart.setContent(new String(req.getResponseBody()), req.getResponseHeader("Content-Type").getValue()); newMsg.addPart(newPart); } env.setMessage(newMsg); final Iterator addressIterator = env.getAddresses(); if (addressIterator.hasNext()) ctx.addOutgoing(env); } } catch (final Exception e) { throw new XQServiceException(e); } }
From source file:com.xmlcalabash.library.ApacheHttpRequest.java
private void readBodyContent(TreeWriter tree, InputStream bodyStream, HttpMethodBase method) throws SaxonApiException, IOException { // Find the content type String contentType = getContentType(method); String charset = method.getResponseCharSet(); String boundary = null;// w w w . ja v a 2 s .c o m if (overrideContentType != null) { contentType = overrideContentType; } if (contentType.startsWith("multipart/")) { boundary = getContentBoundary(method); tree.addStartElement(XProcConstants.c_multipart); tree.addAttribute(_content_type, getFullContentType(method)); tree.addAttribute(_boundary, boundary); tree.startContent(); for (Header header : method.getResponseHeaders()) { // FIXME: what about parameters? if (header.getName().toLowerCase().equals("transfer-encoding")) { // nop; } else { tree.addStartElement(XProcConstants.c_header); tree.addAttribute(_name, header.getName()); tree.addAttribute(_value, header.getValue()); tree.startContent(); tree.addEndElement(); } } MIMEReader reader = new MIMEReader(bodyStream, boundary); boolean done = false; while (reader.readHeaders()) { Header pctype = reader.getHeader("Content-Type"); Header pclen = reader.getHeader("Content-Length"); contentType = getHeaderValue(pctype); charset = getContentCharset(pctype); String partType = getHeaderValue(pctype); InputStream partStream = null; if (pclen != null) { int len = Integer.parseInt(getHeaderValue(pclen)); partStream = reader.readBodyPart(len); } else { partStream = reader.readBodyPart(); } tree.addStartElement(XProcConstants.c_body); tree.addAttribute(_content_type, contentType); if (!xmlContentType(contentType) && !textContentType(contentType)) { tree.addAttribute(_encoding, "base64"); } tree.startContent(); if (xmlContentType(partType)) { BufferedReader preader = new BufferedReader(new InputStreamReader(partStream, charset)); // Read it as XML SAXSource source = new SAXSource(new InputSource(preader)); DocumentBuilder builder = runtime.getProcessor().newDocumentBuilder(); tree.addSubtree(builder.build(source)); } else if (textContentType(partType)) { BufferedReader preader = new BufferedReader(new InputStreamReader(partStream, charset)); // Read it as text char buf[] = new char[bufSize]; int len = preader.read(buf, 0, bufSize); while (len >= 0) { // I'm unsure about this. If I'm reading text and injecting it into XML, // I think I need to change CR/LF pairs (and CR not followed by LF) into // plain LFs. char fbuf[] = new char[bufSize]; char flen = 0; for (int pos = 0; pos < len; pos++) { if (buf[pos] == '\r') { if (pos + 1 == len) { // FIXME: Check for CR/LF pairs that cross a buffer boundary! // Assume it's part of a CR/LF pair... } else { if (buf[pos + 1] == '\n') { // nop } else { fbuf[flen++] = '\n'; } } } else { fbuf[flen++] = buf[pos]; } } tree.addText(new String(fbuf, 0, flen)); len = preader.read(buf, 0, bufSize); } } else { // Read it as binary byte bytes[] = new byte[bufSize]; int pos = 0; int readLen = bufSize; int len = partStream.read(bytes, 0, bufSize); while (len >= 0) { pos += len; readLen -= len; if (readLen == 0) { tree.addText(Base64.encodeBytes(bytes)); pos = 0; readLen = bufSize; } len = partStream.read(bytes, pos, readLen); } if (pos > 0) { byte lastBytes[] = new byte[pos]; System.arraycopy(bytes, 0, lastBytes, 0, pos); tree.addText(Base64.encodeBytes(lastBytes)); } tree.addText("\n"); // FIXME: should we be doing this? } tree.addEndElement(); } tree.addEndElement(); } else { if (xmlContentType(contentType)) { readBodyContentPart(tree, bodyStream, contentType, charset); } else { tree.addStartElement(XProcConstants.c_body); tree.addAttribute(_content_type, getFullContentType(method)); if (!xmlContentType(contentType) && !textContentType(contentType)) { tree.addAttribute(_encoding, "base64"); } tree.startContent(); readBodyContentPart(tree, bodyStream, contentType, charset); tree.addEndElement(); } } }
From source file:com.xmlcalabash.library.ApacheHttpRequest.java
public void run() throws SaxonApiException { super.run();//from w w w . j ava2 s . com XdmNode requestDoc = source.read(); XdmNode start = S9apiUtils.getDocumentElement(requestDoc); if (!c_request.equals(start.getNodeName())) { throw new UnsupportedOperationException("Not a c:http-request!"); } // Check for valid attributes XdmSequenceIterator iter = start.axisIterator(Axis.ATTRIBUTE); boolean ok = true; while (iter.hasNext()) { XdmNode attr = (XdmNode) iter.next(); QName name = attr.getNodeName(); if (_method.equals(name) || _href.equals(name) || _detailed.equals(name) || _status_only.equals(name) || _username.equals(name) || _password.equals(name) || _auth_method.equals(name) || _send_authorization.equals(name) || _override_content_type.equals(name)) { // nop } else { if (XMLConstants.DEFAULT_NS_PREFIX.equals(name.getNamespaceURI())) { throw new XProcException("Unsupported attribute on c:http-request: " + name); } } } method = start.getAttributeValue(_method); statusOnly = "true".equals(start.getAttributeValue(_status_only)); detailed = "true".equals(start.getAttributeValue(_detailed)); overrideContentType = start.getAttributeValue(_override_content_type); if (start.getAttributeValue(_href) == null) { throw new XProcException("The 'href' attribute must be specified on p:http-request"); } requestURI = start.getBaseURI().resolve(start.getAttributeValue(_href)); if ("file".equals(requestURI.getScheme())) { doFile(); return; } client = new HttpClient(); String timeOutStr = step.getExtensionAttribute(cx_timeout); if (timeOutStr != null) { HttpMethodParams params = client.getParams(); params.setSoTimeout(Integer.parseInt(timeOutStr)); } ProxySelector proxySelector = ProxySelector.getDefault(); List<Proxy> plist = proxySelector.select(requestURI); // I have no idea what I'm expected to do if I get more than one... if (plist.size() > 0) { Proxy proxy = plist.get(0); switch (proxy.type()) { case DIRECT: // nop; break; case HTTP: // This can't cause a ClassCastException, right? InetSocketAddress addr = (InetSocketAddress) proxy.address(); String host = addr.getHostName(); int port = addr.getPort(); client.getHostConfiguration().setProxy(host, port); break; default: // FIXME: send out a log message break; } } if (start.getAttributeValue(_username) != null) { String user = start.getAttributeValue(_username); String pass = start.getAttributeValue(_password); String meth = start.getAttributeValue(_auth_method); if (meth == null || !("basic".equals(meth.toLowerCase()) || "digest".equals(meth.toLowerCase()))) { throw XProcException.stepError(3, "Unsupported auth-method: " + meth); } String host = requestURI.getHost(); int port = requestURI.getPort(); AuthScope scope = new AuthScope(host, port); UsernamePasswordCredentials cred = new UsernamePasswordCredentials(user, pass); client.getState().setCredentials(scope, cred); } iter = start.axisIterator(Axis.CHILD); XdmNode body = null; while (iter.hasNext()) { XdmNode event = (XdmNode) iter.next(); // FIXME: What about non-whitespace text nodes? if (event.getNodeKind() == XdmNodeKind.ELEMENT) { if (body != null) { throw new UnsupportedOperationException("Elements follow c:multipart or c:body"); } if (XProcConstants.c_header.equals(event.getNodeName())) { headers.add(new Header(event.getAttributeValue(_name), event.getAttributeValue(_value))); } else if (XProcConstants.c_multipart.equals(event.getNodeName()) || XProcConstants.c_body.equals(event.getNodeName())) { body = event; } else { throw new UnsupportedOperationException("Unexpected request element: " + event.getNodeName()); } } } HttpMethodBase httpResult; if (method == null) { throw new XProcException("Method must be specified."); } if ("get".equals(method.toLowerCase())) { httpResult = doGet(); } else if ("post".equals(method.toLowerCase())) { httpResult = doPost(body); } else if ("put".equals(method.toLowerCase())) { httpResult = doPut(body); } else if ("head".equals(method.toLowerCase())) { httpResult = doHead(); } else { throw new UnsupportedOperationException("Unrecognized http method: " + method); } TreeWriter tree = new TreeWriter(runtime); tree.startDocument(requestURI); try { // Execute the method. int statusCode = client.executeMethod(httpResult); String contentType = getContentType(httpResult); if (overrideContentType != null) { if ((xmlContentType(contentType) && overrideContentType.startsWith("image/")) || (contentType.startsWith("text/") && overrideContentType.startsWith("image/")) || (contentType.startsWith("image/") && xmlContentType(overrideContentType)) || (contentType.startsWith("image/") && overrideContentType.startsWith("text/")) || (contentType.startsWith("multipart/") && !overrideContentType.startsWith("multipart/")) || (!contentType.startsWith("multipart/") && overrideContentType.startsWith("multipart/"))) { throw XProcException.stepError(30); } //System.err.println(overrideContentType + " overrides " + contentType); contentType = overrideContentType; } if (detailed) { tree.addStartElement(XProcConstants.c_response); tree.addAttribute(_status, "" + statusCode); tree.startContent(); for (Header header : httpResult.getResponseHeaders()) { // I don't understand why/how HeaderElement parsing works. I get very weird results. // So I'm just going to go the long way around... String h = header.toString(); int cp = h.indexOf(":"); String name = header.getName(); String value = h.substring(cp + 1).trim(); tree.addStartElement(XProcConstants.c_header); tree.addAttribute(_name, name); tree.addAttribute(_value, value); tree.startContent(); tree.addEndElement(); } if (statusOnly) { // Skip reading the result } else { // Read the response body. InputStream bodyStream = httpResult.getResponseBodyAsStream(); readBodyContent(tree, bodyStream, httpResult); } tree.addEndElement(); } else { if (statusOnly) { // Skip reading the result } else { // Read the response body. InputStream bodyStream = httpResult.getResponseBodyAsStream(); readBodyContent(tree, bodyStream, httpResult); } } } catch (Exception e) { throw new XProcException(e); } finally { // Release the connection. httpResult.releaseConnection(); } tree.endDocument(); XdmNode resultNode = tree.getResult(); result.write(resultNode); }
From source file:com.esri.gpt.framework.http.HttpClientRequest.java
/** * Executes the HTTP request./* w ww. j a v a2 s . c om*/ * @throws IOException if an Exception occurs */ public void execute() throws IOException { // initialize this.executionLog.setLength(0); StringBuffer log = this.executionLog; ResponseInfo respInfo = this.getResponseInfo(); respInfo.reset(); InputStream responseStream = null; HttpMethodBase method = null; try { log.append("HTTP Client Request\n").append(this.getUrl()); // make the Apache HTTPClient HttpClient client = this.batchHttpClient; if (client == null) { client = new HttpClient(); boolean alwaysClose = Val.chkBool(Val.chkStr(ApplicationContext.getInstance().getConfiguration() .getCatalogConfiguration().getParameters().getValue("httpClient.alwaysClose")), false); if (alwaysClose) { client.setHttpConnectionManager(new SimpleHttpConnectionManager(true)); } } // setting timeout info client.getHttpConnectionManager().getParams().setConnectionTimeout(getConnectionTimeOutMs()); client.getHttpConnectionManager().getParams().setSoTimeout(getResponseTimeOutMs()); // setting retries int retries = this.getRetries(); // create the client and method, apply authentication and proxy settings method = this.createMethod(); //method.setFollowRedirects(true); if (retries > -1) { // TODO: not taking effect yet? DefaultHttpMethodRetryHandler retryHandler = new DefaultHttpMethodRetryHandler(retries, true); client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, retryHandler); method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, retryHandler); } this.applyAuthAndProxySettings(client, this.getUrl()); // execute the method, determine basic information about the response respInfo.setResponseCode(client.executeMethod(method)); this.determineResponseInfo(method); // collect logging info if (LOGGER.isLoggable(Level.FINER)) { log.append("\n>>").append(method.getStatusLine()); log.append("\n--Request Header"); for (Header hdr : method.getRequestHeaders()) { log.append("\n ").append(hdr.getName() + ": " + hdr.getValue()); } log.append("\n--Response Header"); for (Header hdr : method.getResponseHeaders()) { log.append("\n ").append(hdr.getName() + ": " + hdr.getValue()); } //log.append(" responseCode=").append(this.getResponseInfo().getResponseCode()); //log.append(" responseContentType=").append(this.getResponseInfo().getContentType()); //log.append(" responseContentEncoding=").append(this.getResponseInfo().getContentEncoding()); //log.append(" responseContentLength=").append(this.getResponseInfo().getContentLength()); if (this.getContentProvider() != null) { String loggable = this.getContentProvider().getLoggableContent(); if (loggable != null) { log.append("\n--Request Content------------------------------------\n").append(loggable); } } } // throw an exception if an error is encountered if ((respInfo.getResponseCode() < 200) || (respInfo.getResponseCode() >= 300)) { String msg = "HTTP Request failed: " + method.getStatusLine(); if (respInfo.getResponseCode() == HttpStatus.SC_UNAUTHORIZED) { AuthState authState = method.getHostAuthState(); AuthScheme authScheme = authState.getAuthScheme(); HttpClient401Exception authException = new HttpClient401Exception(msg); authException.setUrl(this.getUrl()); authException.setRealm(authState.getRealm()); authException.setScheme(authScheme.getSchemeName()); if ((authException.getRealm() == null) || (authException.getRealm().length() == 0)) { authException.setRealm(authException.generateHostBasedRealm()); } throw authException; } else { throw new HttpClientException(respInfo.getResponseCode(), msg); } } // handle the response if (this.getContentHandler() != null) { if (getContentHandler().onBeforeReadResponse(this)) { responseStream = getResponseStream(method); if (responseStream != null) { this.getContentHandler().readResponse(this, responseStream); } } // log thre response content String loggable = this.getContentHandler().getLoggableContent(); long nBytesRead = this.getResponseInfo().getBytesRead(); long nCharsRead = this.getResponseInfo().getCharactersRead(); if ((nBytesRead >= 0) || (nCharsRead >= 0) || (loggable != null)) { log.append("\n--Response Content------------------------------------"); if (nBytesRead >= 0) log.append("\n(").append(nBytesRead).append(" bytes read)"); if (nCharsRead >= 0) log.append("\n(").append(nCharsRead).append(" characters read)"); if (loggable != null) log.append("\n").append(loggable); } } } finally { // cleanup try { if (responseStream != null) responseStream.close(); } catch (Throwable t) { LOGGER.log(Level.SEVERE, "Unable to close HTTP response stream.", t); } try { if (method != null) method.releaseConnection(); } catch (Throwable t) { LOGGER.log(Level.SEVERE, "Unable to release HttpMethod", t); } // log the request/response if (LOGGER.isLoggable(Level.FINER)) { LOGGER.finer(this.getExecutionLog().toString()); } } }
From source file:com.xmlcalabash.library.HttpRequest.java
public void gorun() throws SaxonApiException { super.gorun(); XdmNode requestDoc = source.read(stepContext); XdmNode start = S9apiUtils.getDocumentElement(requestDoc); if (!c_request.equals(start.getNodeName())) { throw XProcException.stepError(40); }//from w ww .j a v a2s . c o m // Check for valid attributes XdmSequenceIterator iter = start.axisIterator(Axis.ATTRIBUTE); boolean ok = true; while (iter.hasNext()) { XdmNode attr = (XdmNode) iter.next(); QName name = attr.getNodeName(); if (_method.equals(name) || _href.equals(name) || _detailed.equals(name) || _status_only.equals(name) || _username.equals(name) || _password.equals(name) || _auth_method.equals(name) || _send_authorization.equals(name) || _override_content_type.equals(name)) { // nop } else { if (XMLConstants.DEFAULT_NS_PREFIX.equals(name.getNamespaceURI())) { throw new XProcException(step.getNode(), "Unsupported attribute on c:request for p:http-request: " + name); } } } String send = step.getExtensionAttribute(cx_send_binary); encodeBinary = !"true".equals(send); method = start.getAttributeValue(_method); statusOnly = "true".equals(start.getAttributeValue(_status_only)); detailed = "true".equals(start.getAttributeValue(_detailed)); overrideContentType = start.getAttributeValue(_override_content_type); if (method == null) { throw XProcException.stepError(6); } if (statusOnly && !detailed) { throw XProcException.stepError(4); } if (start.getAttributeValue(_href) == null) { throw new XProcException(step.getNode(), "The 'href' attribute must be specified on c:request for p:http-request"); } requestURI = start.getBaseURI().resolve(start.getAttributeValue(_href)); if ("file".equals(requestURI.getScheme())) { doFile(); return; } // What about cookies String saveCookieKey = step.getExtensionAttribute(cx_save_cookies); String useCookieKeys = step.getExtensionAttribute(cx_use_cookies); String cookieKey = step.getExtensionAttribute(cx_cookies); if (saveCookieKey == null) { saveCookieKey = cookieKey; } if (useCookieKeys == null) { useCookieKeys = cookieKey; } client = new HttpClient(); client.getParams().setCookiePolicy(CookiePolicy.RFC_2109); client.getParams().setParameter("http.protocol.single-cookie-header", true); HttpState state = client.getState(); if (useCookieKeys != null) { for (String key : useCookieKeys.split("\\s+")) { for (Cookie cookie : runtime.getCookies(key)) { state.addCookie(cookie); } } } String timeOutStr = step.getExtensionAttribute(cx_timeout); if (timeOutStr != null) { HttpMethodParams params = client.getParams(); params.setSoTimeout(Integer.parseInt(timeOutStr)); } ProxySelector proxySelector = ProxySelector.getDefault(); List<Proxy> plist = proxySelector.select(requestURI); // I have no idea what I'm expected to do if I get more than one... if (plist.size() > 0) { Proxy proxy = plist.get(0); switch (proxy.type()) { case DIRECT: // nop; break; case HTTP: // This can't cause a ClassCastException, right? InetSocketAddress addr = (InetSocketAddress) proxy.address(); String host = addr.getHostName(); int port = addr.getPort(); client.getHostConfiguration().setProxy(host, port); break; default: // FIXME: send out a log message break; } } if (start.getAttributeValue(_username) != null) { String user = start.getAttributeValue(_username); String pass = start.getAttributeValue(_password); String meth = start.getAttributeValue(_auth_method); if (meth == null || !("basic".equals(meth.toLowerCase()) || "digest".equals(meth.toLowerCase()))) { throw XProcException.stepError(3, "Unsupported auth-method: " + meth); } String host = requestURI.getHost(); int port = requestURI.getPort(); AuthScope scope = new AuthScope(host, port); UsernamePasswordCredentials cred = new UsernamePasswordCredentials(user, pass); client.getState().setCredentials(scope, cred); if ("basic".equals(meth.toLowerCase())) { client.getParams().setAuthenticationPreemptive(true); } } iter = start.axisIterator(Axis.CHILD); XdmNode body = null; while (iter.hasNext()) { XdmNode event = (XdmNode) iter.next(); // FIXME: What about non-whitespace text nodes? if (event.getNodeKind() == XdmNodeKind.ELEMENT) { if (body != null) { throw new UnsupportedOperationException("Elements follow c:multipart or c:body"); } if (XProcConstants.c_header.equals(event.getNodeName())) { String name = event.getAttributeValue(_name); if (name == null) { continue; // this can't happen, right? } if (name.toLowerCase().equals("content-type")) { // We'll deal with the content-type header later headerContentType = event.getAttributeValue(_value).toLowerCase(); } else { headers.add(new Header(event.getAttributeValue(_name), event.getAttributeValue(_value))); } } else if (XProcConstants.c_multipart.equals(event.getNodeName()) || XProcConstants.c_body.equals(event.getNodeName())) { body = event; } else { throw new UnsupportedOperationException("Unexpected request element: " + event.getNodeName()); } } } String lcMethod = method.toLowerCase(); // You can only have a body on PUT or POST if (body != null && !("put".equals(lcMethod) || "post".equals(lcMethod))) { throw XProcException.stepError(5); } HttpMethodBase httpResult; if ("get".equals(lcMethod)) { httpResult = doGet(); } else if ("post".equals(lcMethod)) { httpResult = doPost(body); } else if ("put".equals(lcMethod)) { httpResult = doPut(body); } else if ("head".equals(lcMethod)) { httpResult = doHead(); } else if ("delete".equals(lcMethod)) { httpResult = doDelete(); } else { throw new UnsupportedOperationException("Unrecognized http method: " + method); } TreeWriter tree = new TreeWriter(runtime); tree.startDocument(requestURI); try { // Execute the method. int statusCode = client.executeMethod(httpResult); // Deal with cookies if (saveCookieKey != null) { runtime.clearCookies(saveCookieKey); state = client.getState(); Cookie[] cookies = state.getCookies(); for (Cookie cookie : cookies) { runtime.addCookie(saveCookieKey, cookie); } } String contentType = getContentType(httpResult); if (overrideContentType != null) { if ((xmlContentType(contentType) && overrideContentType.startsWith("image/")) || (contentType.startsWith("text/") && overrideContentType.startsWith("image/")) || (contentType.startsWith("image/") && xmlContentType(overrideContentType)) || (contentType.startsWith("image/") && overrideContentType.startsWith("text/")) || (contentType.startsWith("multipart/") && !overrideContentType.startsWith("multipart/")) || (!contentType.startsWith("multipart/") && overrideContentType.startsWith("multipart/"))) { throw XProcException.stepError(30); } //System.err.println(overrideContentType + " overrides " + contentType); contentType = overrideContentType; } if (detailed) { tree.addStartElement(XProcConstants.c_response); tree.addAttribute(_status, "" + statusCode); tree.startContent(); for (Header header : httpResult.getResponseHeaders()) { // I don't understand why/how HeaderElement parsing works. I get very weird results. // So I'm just going to go the long way around... String h = header.toString(); int cp = h.indexOf(":"); String name = header.getName(); String value = h.substring(cp + 1).trim(); tree.addStartElement(XProcConstants.c_header); tree.addAttribute(_name, name); tree.addAttribute(_value, value); tree.startContent(); tree.addEndElement(); } if (statusOnly) { // Skip reading the result } else { // Read the response body. InputStream bodyStream = httpResult.getResponseBodyAsStream(); if (bodyStream != null) { readBodyContent(tree, bodyStream, httpResult); } } tree.addEndElement(); } else { if (statusOnly) { // Skip reading the result } else { // Read the response body. InputStream bodyStream = httpResult.getResponseBodyAsStream(); if (bodyStream != null) { readBodyContent(tree, bodyStream, httpResult); } else { throw XProcException.dynamicError(6); } } } } catch (Exception e) { throw new XProcException(e); } finally { // Release the connection. httpResult.releaseConnection(); } tree.endDocument(); XdmNode resultNode = tree.getResult(); result.write(stepContext, resultNode); }
From source file:com.snaker.DownloadManager.java
private Runnable createRunnable(final Downloader d) { final Task task = d.getTask(); return new Runnable() { @Override//from w w w.j a v a2 s . c o m public void run() { logger.info("start download:" + d.getUrl()); HttpClient client = clients.get(task.getId()); DownloadHandler handler = d.getHandler(); HttpMethodBase m = null; d.setStatus(Status.STARTED); d.setStartTime(System.currentTimeMillis()); try { String url = d.getUrl(); if (d.isGet()) { GetMethod get = new GetMethod(url); m = get; } else { final String requestCharset = d.getRequestCharset(); PostMethod post = new PostMethod(url) { public String getRequestCharSet() { if (requestCharset != null) return requestCharset; else return super.getRequestCharSet(); } public boolean getFollowRedirects() { return d.isFollowRedirects(); } }; if (requestCharset != null) { post.setRequestHeader("ContentType", "application/x-www-form-urlencoded;charset=" + requestCharset); } DownloadParams parms = d.getParms(); if (parms != null) post.setRequestBody(parms.toNVP()); m = post; } { // set the headers m.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:8.0.1) Gecko/20100101 Firefox/8.0.1"); m.setRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); m.setRequestHeader("Accept-Language", "en-us,zh-cn;q=0.5"); m.setRequestHeader("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7"); m.setRequestHeader("Referer", url); } client.executeMethod(m); //check status int sc = m.getStatusCode(); d.setStatusCode(sc); if (isBadStatusCode(sc)) { logger.error("download failed,url:" + d.getUrl() + ",Status Code:" + sc); d.setStatus(Status.FAILED); d.setDescription(m.getStatusText()); return; } else if (sc == 404 || sc == 410) { d.setStatus(Status.FINISHED); d.setDescription("NOT FOUND"); return; } long size = m.getResponseContentLength(); d.setFileSize(size); // get File Name if (d.getFileName() == null) { Header h = m.getResponseHeader("Content-Disposition"); String fileName = null; if (h != null) { String f = h.getValue(); int tag = f.indexOf("filename="); if (tag != -1 && tag != f.length() - 1) fileName = f.substring(tag + 1); } if (fileName == null || fileName.length() == 0) { int tag1 = url.lastIndexOf("/"); int tag2 = url.lastIndexOf("?"); if (tag1 != -1 && tag1 != url.length() - 1) { if (tag2 > tag1) { fileName = url.substring(tag1 + 1, tag2); } else { fileName = url.substring(tag1 + 1); } } } d.setFileName(fileName); } // set the all headers Header[] headers = m.getResponseHeaders(); if (headers != null) { for (Header header : headers) { d.addResponseHeader(header.getName(), header.getValue()); } } d.setStatus(Status.RUNNING); // recv the body if (handler == null) { byte[] content = m.getResponseBody(); int len = content.length; d.setFileSize(len); d.setReceived(len); d.setResponseCharset(m.getResponseCharSet()); d.setResponseBody(content); } else { InputStream is = m.getResponseBodyAsStream(); handler.start(d); byte[] buffer = new byte[102400]; long count = 0; while (true) { int r = is.read(buffer); if (r > 0) { count += r; d.setReceived(count); handler.handle(buffer, r); } else { break; } } is.close(); } d.setStatus(Status.FINISHED); } catch (Exception e) { logger.error("download failed,url:" + d.getUrl(), e); d.setStatus(Status.FAILED); d.setDescription(e.getMessage()); } finally { m.releaseConnection(); if (handler != null) { handler.stop(); } downloadings.remove(d); d.setEndTime(System.currentTimeMillis()); while (downloaded.size() >= maxDownloadedCount) { downloaded.poll(); } downloaded.offer(d); task.downloadFininshed(d); } } }; }