List of usage examples for org.apache.http.client.methods HttpRequestBase getURI
public URI getURI()
From source file:com.eviware.soapui.impl.wsdl.submit.filters.HttpRequestFilter.java
@SuppressWarnings("deprecation") @Override/*from ww w . jav a 2 s. c o m*/ public void filterHttpRequest(SubmitContext context, HttpRequestInterface<?> request) { HttpRequestBase httpMethod = (HttpRequestBase) context.getProperty(BaseHttpRequestTransport.HTTP_METHOD); String path = PropertyExpander.expandProperties(context, request.getPath()); StringBuffer query = new StringBuffer(); String encoding = System.getProperty("soapui.request.encoding", StringUtils.unquote(request.getEncoding())); StringToStringMap responseProperties = (StringToStringMap) context .getProperty(BaseHttpRequestTransport.RESPONSE_PROPERTIES); MimeMultipart formMp = "multipart/form-data".equals(request.getMediaType()) && httpMethod instanceof HttpEntityEnclosingRequestBase ? new MimeMultipart() : null; RestParamsPropertyHolder params = request.getParams(); for (int c = 0; c < params.getPropertyCount(); c++) { RestParamProperty param = params.getPropertyAt(c); String value = PropertyExpander.expandProperties(context, param.getValue()); responseProperties.put(param.getName(), value); List<String> valueParts = sendEmptyParameters(request) || (!StringUtils.hasContent(value) && param.getRequired()) ? RestUtils.splitMultipleParametersEmptyIncluded(value, request.getMultiValueDelimiter()) : RestUtils.splitMultipleParameters(value, request.getMultiValueDelimiter()); // skip HEADER and TEMPLATE parameter encoding (TEMPLATE is encoded by // the URI handling further down) if (value != null && param.getStyle() != ParameterStyle.HEADER && param.getStyle() != ParameterStyle.TEMPLATE && !param.isDisableUrlEncoding()) { try { if (StringUtils.hasContent(encoding)) { value = URLEncoder.encode(value, encoding); for (int i = 0; i < valueParts.size(); i++) valueParts.set(i, URLEncoder.encode(valueParts.get(i), encoding)); } else { value = URLEncoder.encode(value); for (int i = 0; i < valueParts.size(); i++) valueParts.set(i, URLEncoder.encode(valueParts.get(i))); } } catch (UnsupportedEncodingException e1) { SoapUI.logError(e1); value = URLEncoder.encode(value); for (int i = 0; i < valueParts.size(); i++) valueParts.set(i, URLEncoder.encode(valueParts.get(i))); } // URLEncoder replaces space with "+", but we want "%20". value = value.replaceAll("\\+", "%20"); for (int i = 0; i < valueParts.size(); i++) valueParts.set(i, valueParts.get(i).replaceAll("\\+", "%20")); } if (param.getStyle() == ParameterStyle.QUERY && !sendEmptyParameters(request)) { if (!StringUtils.hasContent(value) && !param.getRequired()) continue; } switch (param.getStyle()) { case HEADER: for (String valuePart : valueParts) httpMethod.addHeader(param.getName(), valuePart); break; case QUERY: if (formMp == null || !request.isPostQueryString()) { for (String valuePart : valueParts) { if (query.length() > 0) query.append('&'); query.append(URLEncoder.encode(param.getName())); query.append('='); if (StringUtils.hasContent(valuePart)) query.append(valuePart); } } else { try { addFormMultipart(request, formMp, param.getName(), responseProperties.get(param.getName())); } catch (MessagingException e) { SoapUI.logError(e); } } break; case TEMPLATE: try { value = getEncodedValue(value, encoding, param.isDisableUrlEncoding(), request.getSettings().getBoolean(HttpSettings.ENCODED_URLS)); path = path.replaceAll("\\{" + param.getName() + "\\}", value == null ? "" : value); } catch (UnsupportedEncodingException e) { SoapUI.logError(e); } break; case MATRIX: try { value = getEncodedValue(value, encoding, param.isDisableUrlEncoding(), request.getSettings().getBoolean(HttpSettings.ENCODED_URLS)); } catch (UnsupportedEncodingException e) { SoapUI.logError(e); } if (param.getType().equals(XmlBoolean.type.getName())) { if (value.toUpperCase().equals("TRUE") || value.equals("1")) { path += ";" + param.getName(); } } else { path += ";" + param.getName(); if (StringUtils.hasContent(value)) { path += "=" + value; } } case PLAIN: break; } } if (request.getSettings().getBoolean(HttpSettings.FORWARD_SLASHES)) path = PathUtils.fixForwardSlashesInPath(path); if (PathUtils.isHttpPath(path)) { try { // URI(String) automatically URLencodes the input, so we need to // decode it first... URI uri = new URI(path, request.getSettings().getBoolean(HttpSettings.ENCODED_URLS)); context.setProperty(BaseHttpRequestTransport.REQUEST_URI, uri); java.net.URI oldUri = httpMethod.getURI(); httpMethod.setURI(new java.net.URI(oldUri.getScheme(), oldUri.getUserInfo(), oldUri.getHost(), oldUri.getPort(), (uri.getPath()) == null ? "/" : uri.getPath(), oldUri.getQuery(), oldUri.getFragment())); } catch (Exception e) { SoapUI.logError(e); } } else if (StringUtils.hasContent(path)) { try { java.net.URI oldUri = httpMethod.getURI(); String pathToSet = StringUtils.hasContent(oldUri.getRawPath()) && !"/".equals(oldUri.getRawPath()) ? oldUri.getRawPath() + path : path; java.net.URI newUri = URIUtils.createURI(oldUri.getScheme(), oldUri.getHost(), oldUri.getPort(), pathToSet, oldUri.getQuery(), oldUri.getFragment()); httpMethod.setURI(newUri); context.setProperty(BaseHttpRequestTransport.REQUEST_URI, new URI(newUri.toString(), request.getSettings().getBoolean(HttpSettings.ENCODED_URLS))); } catch (Exception e) { SoapUI.logError(e); } } if (query.length() > 0 && !request.isPostQueryString()) { try { java.net.URI oldUri = httpMethod.getURI(); httpMethod.setURI(URIUtils.createURI(oldUri.getScheme(), oldUri.getHost(), oldUri.getPort(), oldUri.getRawPath(), query.toString(), oldUri.getFragment())); } catch (Exception e) { SoapUI.logError(e); } } if (request instanceof RestRequest) { String acceptEncoding = ((RestRequest) request).getAccept(); if (StringUtils.hasContent(acceptEncoding)) { httpMethod.setHeader("Accept", acceptEncoding); } } if (formMp != null) { // create request message try { if (request.hasRequestBody() && httpMethod instanceof HttpEntityEnclosingRequest) { String requestContent = PropertyExpander.expandProperties(context, request.getRequestContent(), request.isEntitizeProperties()); if (StringUtils.hasContent(requestContent)) { initRootPart(request, requestContent, formMp); } } for (Attachment attachment : request.getAttachments()) { MimeBodyPart part = new PreencodedMimeBodyPart("binary"); if (attachment instanceof FileAttachment<?>) { String name = attachment.getName(); if (StringUtils.hasContent(attachment.getContentID()) && !name.equals(attachment.getContentID())) name = attachment.getContentID(); part.setDisposition( "form-data; name=\"" + name + "\"; filename=\"" + attachment.getName() + "\""); } else part.setDisposition("form-data; name=\"" + attachment.getName() + "\""); part.setDataHandler(new DataHandler(new AttachmentDataSource(attachment))); formMp.addBodyPart(part); } MimeMessage message = new MimeMessage(AttachmentUtils.JAVAMAIL_SESSION); message.setContent(formMp); message.saveChanges(); RestRequestMimeMessageRequestEntity mimeMessageRequestEntity = new RestRequestMimeMessageRequestEntity( message, request); ((HttpEntityEnclosingRequest) httpMethod).setEntity(mimeMessageRequestEntity); httpMethod.setHeader("Content-Type", mimeMessageRequestEntity.getContentType().getValue()); httpMethod.setHeader("MIME-Version", "1.0"); } catch (Throwable e) { SoapUI.logError(e); } } else if (request.hasRequestBody() && httpMethod instanceof HttpEntityEnclosingRequest) { if (StringUtils.hasContent(request.getMediaType())) httpMethod.setHeader("Content-Type", getContentTypeHeader(request.getMediaType(), encoding)); if (request.isPostQueryString()) { try { ((HttpEntityEnclosingRequest) httpMethod).setEntity(new StringEntity(query.toString())); } catch (UnsupportedEncodingException e) { SoapUI.logError(e); } } else { String requestContent = PropertyExpander.expandProperties(context, request.getRequestContent(), request.isEntitizeProperties()); List<Attachment> attachments = new ArrayList<Attachment>(); for (Attachment attachment : request.getAttachments()) { if (attachment.getContentType().equals(request.getMediaType())) { attachments.add(attachment); } } if (StringUtils.hasContent(requestContent) && attachments.isEmpty()) { try { byte[] content = encoding == null ? requestContent.getBytes() : requestContent.getBytes(encoding); ((HttpEntityEnclosingRequest) httpMethod).setEntity(new ByteArrayEntity(content)); } catch (UnsupportedEncodingException e) { ((HttpEntityEnclosingRequest) httpMethod) .setEntity(new ByteArrayEntity(requestContent.getBytes())); } } else if (attachments.size() > 0) { try { MimeMultipart mp = null; if (StringUtils.hasContent(requestContent)) { mp = new MimeMultipart(); initRootPart(request, requestContent, mp); } else if (attachments.size() == 1) { ((HttpEntityEnclosingRequest) httpMethod) .setEntity(new InputStreamEntity(attachments.get(0).getInputStream(), -1)); httpMethod.setHeader("Content-Type", getContentTypeHeader(request.getMediaType(), encoding)); } if (((HttpEntityEnclosingRequest) httpMethod).getEntity() == null) { if (mp == null) mp = new MimeMultipart(); // init mimeparts AttachmentUtils.addMimeParts(request, attachments, mp, new StringToStringMap()); // create request message MimeMessage message = new MimeMessage(AttachmentUtils.JAVAMAIL_SESSION); message.setContent(mp); message.saveChanges(); RestRequestMimeMessageRequestEntity mimeMessageRequestEntity = new RestRequestMimeMessageRequestEntity( message, request); ((HttpEntityEnclosingRequest) httpMethod).setEntity(mimeMessageRequestEntity); httpMethod.setHeader("Content-Type", getContentTypeHeader( mimeMessageRequestEntity.getContentType().getValue(), encoding)); httpMethod.setHeader("MIME-Version", "1.0"); } } catch (Exception e) { SoapUI.logError(e); } } } } }
From source file:com.su.search.client.solrj.PaHttpSolrServer.java
public NamedList<Object> request(final SolrRequest request, final ResponseParser processor) throws SolrServerException, IOException { HttpRequestBase method = null; InputStream is = null;//w w w. ja v a 2s . c om SolrParams params = request.getParams(); // modified by wangqiang406 2012-07-27 // ?? if (null != password) { ModifiableSolrParams wparams = new ModifiableSolrParams(params); wparams.set(SimpleIndexClient.KEY_PA_AUTH, password); params = wparams; } Collection<ContentStream> streams = requestWriter.getContentStreams(request); String path = requestWriter.getPath(request); if (path == null || !path.startsWith("/")) { path = DEFAULT_PATH; } ResponseParser parser = request.getResponseParser(); if (parser == null) { parser = this.parser; } // The parser 'wt=' and 'version=' params are used instead of the original // params ModifiableSolrParams wparams = new ModifiableSolrParams(params); wparams.set(CommonParams.WT, parser.getWriterType()); wparams.set(CommonParams.VERSION, parser.getVersion()); if (invariantParams != null) { wparams.add(invariantParams); } params = wparams; int tries = maxRetries + 1; try { while (tries-- > 0) { // Note: since we aren't do intermittent time keeping // ourselves, the potential non-timeout latency could be as // much as tries-times (plus scheduling effects) the given // timeAllowed. try { if (SolrRequest.METHOD.GET == request.getMethod()) { if (streams != null) { throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "GET can't send streams!"); } method = new HttpGet(baseUrl + path + ClientUtils.toQueryString(params, false)); } else if (SolrRequest.METHOD.POST == request.getMethod()) { String url = baseUrl + path; boolean isMultipart = (streams != null && streams.size() > 1); LinkedList<NameValuePair> postParams = new LinkedList<NameValuePair>(); if (streams == null || isMultipart) { HttpPost post = new HttpPost(url); post.setHeader("Content-Charset", "UTF-8"); if (!this.useMultiPartPost && !isMultipart) { post.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); } List<FormBodyPart> parts = new LinkedList<FormBodyPart>(); Iterator<String> iter = params.getParameterNamesIterator(); while (iter.hasNext()) { String p = iter.next(); String[] vals = params.getParams(p); if (vals != null) { for (String v : vals) { if (this.useMultiPartPost || isMultipart) { parts.add(new FormBodyPart(p, new StringBody(v, Charset.forName("UTF-8")))); } else { postParams.add(new BasicNameValuePair(p, v)); } } } } if (isMultipart) { for (ContentStream content : streams) { parts.add(new FormBodyPart(content.getName(), new InputStreamBody(content.getStream(), content.getName()))); } } if (parts.size() > 0) { MultipartEntity entity = new MultipartEntity(HttpMultipartMode.STRICT); for (FormBodyPart p : parts) { entity.addPart(p); } post.setEntity(entity); } else { //not using multipart HttpEntity e; post.setEntity(new UrlEncodedFormEntity(postParams, "UTF-8")); } method = post; } // It is has one stream, it is the post body, put the params in the URL else { String pstr = ClientUtils.toQueryString(params, false); HttpPost post = new HttpPost(url + pstr); // Single stream as body // Using a loop just to get the first one final ContentStream[] contentStream = new ContentStream[1]; for (ContentStream content : streams) { contentStream[0] = content; break; } if (contentStream[0] instanceof RequestWriter.LazyContentStream) { post.setEntity(new InputStreamEntity(contentStream[0].getStream(), -1) { @Override public Header getContentType() { return new BasicHeader("Content-Type", contentStream[0].getContentType()); } @Override public boolean isRepeatable() { return false; } }); } else { post.setEntity(new InputStreamEntity(contentStream[0].getStream(), -1) { @Override public Header getContentType() { return new BasicHeader("Content-Type", contentStream[0].getContentType()); } @Override public boolean isRepeatable() { return false; } }); } method = post; } } else { throw new SolrServerException("Unsupported method: " + request.getMethod()); } } catch (NoHttpResponseException r) { method = null; if (is != null) { is.close(); } // If out of tries then just rethrow (as normal error). if (tries < 1) { throw r; } } } } catch (IOException ex) { throw new SolrServerException("error reading streams", ex); } // TODO: move to a interceptor? method.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS, followRedirects); method.addHeader("User-Agent", AGENT); InputStream respBody = null; try { // Execute the method. final HttpResponse response = httpClient.execute(method); int httpStatus = response.getStatusLine().getStatusCode(); // Read the contents String charset = EntityUtils.getContentCharSet(response.getEntity()); respBody = response.getEntity().getContent(); // handle some http level checks before trying to parse the response switch (httpStatus) { case HttpStatus.SC_OK: break; case HttpStatus.SC_MOVED_PERMANENTLY: case HttpStatus.SC_MOVED_TEMPORARILY: if (!followRedirects) { throw new SolrServerException( "Server at " + getBaseURL() + " sent back a redirect (" + httpStatus + ")."); } break; case HttpStatus.SC_NOT_FOUND: throw new SolrServerException("Server at " + getBaseURL() + " was not found (404)."); default: throw new SolrServerException("Server at " + getBaseURL() + " returned non ok status:" + httpStatus + ", message:" + response.getStatusLine().getReasonPhrase()); } NamedList<Object> rsp = processor.processResponse(respBody, charset); if (httpStatus != HttpStatus.SC_OK) { String reason = null; try { NamedList err = (NamedList) rsp.get("error"); if (err != null) { reason = (String) err.get("msg"); // TODO? get the trace? } } catch (Exception ex) { } if (reason == null) { StringBuilder msg = new StringBuilder(); msg.append(response.getStatusLine().getReasonPhrase()); msg.append("\n\n"); msg.append("request: " + method.getURI()); reason = java.net.URLDecoder.decode(msg.toString(), UTF_8); } throw new SolrException(SolrException.ErrorCode.getErrorCode(httpStatus), reason); } return rsp; } catch (ConnectException e) { throw new SolrServerException("Server refused connection at: " + getBaseURL(), e); } catch (SocketTimeoutException e) { throw new SolrServerException("Timeout occured while waiting response from server at: " + getBaseURL(), e); } catch (IOException e) { throw new SolrServerException("IOException occured when talking to server at: " + getBaseURL(), e); } finally { if (respBody != null) { try { respBody.close(); } catch (Throwable t) { } // ignore } } }
From source file:org.apache.manifoldcf.agents.output.solr.ModifiedHttpSolrServer.java
@Override public NamedList<Object> request(final SolrRequest request, final ResponseParser processor) throws SolrServerException, IOException { HttpRequestBase method = null; InputStream is = null;/*from w ww . j a v a2 s.c o m*/ SolrParams params = request.getParams(); Collection<ContentStream> streams = requestWriter.getContentStreams(request); String path = requestWriter.getPath(request); if (path == null || !path.startsWith("/")) { path = DEFAULT_PATH; } ResponseParser parser = request.getResponseParser(); if (parser == null) { parser = this.parser; } // The parser 'wt=' and 'version=' params are used instead of the original // params ModifiableSolrParams wparams = new ModifiableSolrParams(params); if (parser != null) { wparams.set(CommonParams.WT, parser.getWriterType()); wparams.set(CommonParams.VERSION, parser.getVersion()); } if (invariantParams != null) { wparams.add(invariantParams); } params = wparams; int tries = maxRetries + 1; try { while (tries-- > 0) { // Note: since we aren't do intermittent time keeping // ourselves, the potential non-timeout latency could be as // much as tries-times (plus scheduling effects) the given // timeAllowed. try { if (SolrRequest.METHOD.GET == request.getMethod()) { if (streams != null) { throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "GET can't send streams!"); } method = new HttpGet(baseUrl + path + toQueryString(params, false)); } else if (SolrRequest.METHOD.POST == request.getMethod()) { String url = baseUrl + path; boolean hasNullStreamName = false; if (streams != null) { for (ContentStream cs : streams) { if (cs.getName() == null) { hasNullStreamName = true; break; } } } boolean isMultipart = (this.useMultiPartPost || (streams != null && streams.size() > 1)) && !hasNullStreamName; LinkedList<NameValuePair> postParams = new LinkedList<NameValuePair>(); if (streams == null || isMultipart) { HttpPost post = new HttpPost(url); post.setHeader("Content-Charset", "UTF-8"); if (!isMultipart) { post.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); } List<FormBodyPart> parts = new LinkedList<FormBodyPart>(); Iterator<String> iter = params.getParameterNamesIterator(); while (iter.hasNext()) { String p = iter.next(); String[] vals = params.getParams(p); if (vals != null) { for (String v : vals) { if (isMultipart) { parts.add( new FormBodyPart(p, new StringBody(v, StandardCharsets.UTF_8))); } else { postParams.add(new BasicNameValuePair(p, v)); } } } } if (isMultipart && streams != null) { for (ContentStream content : streams) { String contentType = content.getContentType(); if (contentType == null) { contentType = "application/octet-stream"; // default } String contentName = content.getName(); parts.add(new FormBodyPart(contentName, new InputStreamBody(content.getStream(), contentType, content.getName()))); } } if (parts.size() > 0) { ModifiedMultipartEntity entity = new ModifiedMultipartEntity( HttpMultipartMode.STRICT, null, StandardCharsets.UTF_8); for (FormBodyPart p : parts) { entity.addPart(p); } post.setEntity(entity); } else { //not using multipart post.setEntity(new UrlEncodedFormEntity(postParams, StandardCharsets.UTF_8)); } method = post; } // It is has one stream, it is the post body, put the params in the URL else { String pstr = toQueryString(params, false); HttpPost post = new HttpPost(url + pstr); // Single stream as body // Using a loop just to get the first one final ContentStream[] contentStream = new ContentStream[1]; for (ContentStream content : streams) { contentStream[0] = content; break; } if (contentStream[0] instanceof RequestWriter.LazyContentStream) { post.setEntity(new InputStreamEntity(contentStream[0].getStream(), -1) { @Override public Header getContentType() { return new BasicHeader("Content-Type", contentStream[0].getContentType()); } @Override public boolean isRepeatable() { return false; } }); } else { post.setEntity(new InputStreamEntity(contentStream[0].getStream(), -1) { @Override public Header getContentType() { return new BasicHeader("Content-Type", contentStream[0].getContentType()); } @Override public boolean isRepeatable() { return false; } }); } method = post; } } else { throw new SolrServerException("Unsupported method: " + request.getMethod()); } } catch (NoHttpResponseException r) { method = null; if (is != null) { is.close(); } // If out of tries then just rethrow (as normal error). if (tries < 1) { throw r; } } } } catch (IOException ex) { throw new SolrServerException("error reading streams", ex); } // XXX client already has this set, is this needed? //method.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS, // followRedirects); method.addHeader("User-Agent", AGENT); InputStream respBody = null; boolean shouldClose = true; try { // Execute the method. final HttpResponse response = httpClient.execute(method); int httpStatus = response.getStatusLine().getStatusCode(); // Read the contents respBody = response.getEntity().getContent(); // handle some http level checks before trying to parse the response switch (httpStatus) { case HttpStatus.SC_OK: case HttpStatus.SC_BAD_REQUEST: case HttpStatus.SC_CONFLICT: // 409 break; case HttpStatus.SC_MOVED_PERMANENTLY: case HttpStatus.SC_MOVED_TEMPORARILY: if (!followRedirects) { throw new SolrServerException( "Server at " + getBaseURL() + " sent back a redirect (" + httpStatus + ")."); } break; default: throw new SolrException(SolrException.ErrorCode.getErrorCode(httpStatus), "Server at " + getBaseURL() + " returned non ok status:" + httpStatus + ", message:" + response.getStatusLine().getReasonPhrase()); } if (processor == null) { // no processor specified, return raw stream NamedList<Object> rsp = new NamedList<Object>(); rsp.add("stream", respBody); // Only case where stream should not be closed shouldClose = false; return rsp; } Charset charsetObject = ContentType.getOrDefault(response.getEntity()).getCharset(); String charset; if (charsetObject != null) charset = charsetObject.name(); else charset = "utf-8"; NamedList<Object> rsp = processor.processResponse(respBody, charset); if (httpStatus != HttpStatus.SC_OK) { String reason = null; try { NamedList err = (NamedList) rsp.get("error"); if (err != null) { reason = (String) err.get("msg"); // TODO? get the trace? } } catch (Exception ex) { } if (reason == null) { StringBuilder msg = new StringBuilder(); msg.append(response.getStatusLine().getReasonPhrase()); msg.append("\n\n"); msg.append("request: " + method.getURI()); reason = URLDecoder.decode(msg.toString()); } throw new SolrException(SolrException.ErrorCode.getErrorCode(httpStatus), reason); } return rsp; } catch (ConnectException e) { throw new SolrServerException("Server refused connection at: " + getBaseURL(), e); } catch (SocketTimeoutException e) { throw new SolrServerException("Timeout occured while waiting response from server at: " + getBaseURL(), e); } catch (IOException e) { throw new SolrServerException("IOException occured when talking to server at: " + getBaseURL(), e); } finally { if (respBody != null && shouldClose) { try { respBody.close(); } catch (Throwable t) { } // ignore } } }
From source file:com.nkang.kxmoment.util.SolrUtils.HttpSolrServer.java
public NamedList<Object> request(final SolrRequest request, final ResponseParser processor) throws SolrServerException, IOException { HttpRequestBase method = null; InputStream is = null;//from w w w . ja v a 2 s . co m SolrParams params = request.getParams(); Collection<ContentStream> streams = requestWriter.getContentStreams(request); String path = requestWriter.getPath(request); if (path == null || !path.startsWith("/")) { path = DEFAULT_PATH; } ResponseParser parser = request.getResponseParser(); if (parser == null) { parser = this.parser; } // The parser 'wt=' and 'version=' params are used instead of the original // params ModifiableSolrParams wparams = new ModifiableSolrParams(params); if (parser != null) { wparams.set(CommonParams.WT, parser.getWriterType()); wparams.set(CommonParams.VERSION, parser.getVersion()); } if (invariantParams != null) { wparams.add(invariantParams); } int tries = maxRetries + 1; try { while (tries-- > 0) { // Note: since we aren't do intermittent time keeping // ourselves, the potential non-timeout latency could be as // much as tries-times (plus scheduling effects) the given // timeAllowed. try { if (SolrRequest.METHOD.GET == request.getMethod()) { if (streams != null) { throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "GET can't send streams!"); } method = new HttpGet(baseUrl + path + ClientUtils.toQueryString(wparams, false)); } else if (SolrRequest.METHOD.POST == request.getMethod()) { String url = baseUrl + path; boolean hasNullStreamName = false; if (streams != null) { for (ContentStream cs : streams) { if (cs.getName() == null) { hasNullStreamName = true; break; } } } boolean isMultipart = (this.useMultiPartPost || (streams != null && streams.size() > 1)) && !hasNullStreamName; // only send this list of params as query string params ModifiableSolrParams queryParams = new ModifiableSolrParams(); for (String param : this.queryParams) { String[] value = wparams.getParams(param); if (value != null) { for (String v : value) { queryParams.add(param, v); } wparams.remove(param); } } LinkedList<NameValuePair> postParams = new LinkedList<NameValuePair>(); if (streams == null || isMultipart) { HttpPost post = new HttpPost(url + ClientUtils.toQueryString(queryParams, false)); post.setHeader("Content-Charset", "UTF-8"); if (!isMultipart) { post.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); } List<FormBodyPart> parts = new LinkedList<FormBodyPart>(); Iterator<String> iter = wparams.getParameterNamesIterator(); while (iter.hasNext()) { String p = iter.next(); String[] vals = wparams.getParams(p); if (vals != null) { for (String v : vals) { if (isMultipart) { parts.add(new FormBodyPart(p, new StringBody(v, Charset.forName("UTF-8")))); } else { postParams.add(new BasicNameValuePair(p, v)); } } } } if (isMultipart && streams != null) { for (ContentStream content : streams) { String contentType = content.getContentType(); if (contentType == null) { contentType = BinaryResponseParser.BINARY_CONTENT_TYPE; // default } String name = content.getName(); if (name == null) { name = ""; } parts.add(new FormBodyPart(name, new InputStreamBody(content.getStream(), contentType, content.getName()))); } } if (parts.size() > 0) { MultipartEntity entity = new MultipartEntity(HttpMultipartMode.STRICT); for (FormBodyPart p : parts) { entity.addPart(p); } post.setEntity(entity); } else { //not using multipart post.setEntity(new UrlEncodedFormEntity(postParams, "UTF-8")); } method = post; } // It is has one stream, it is the post body, put the params in the URL else { String pstr = ClientUtils.toQueryString(wparams, false); HttpPost post = new HttpPost(url + pstr); // Single stream as body // Using a loop just to get the first one final ContentStream[] contentStream = new ContentStream[1]; for (ContentStream content : streams) { contentStream[0] = content; break; } if (contentStream[0] instanceof RequestWriter.LazyContentStream) { post.setEntity(new InputStreamEntity(contentStream[0].getStream(), -1) { @Override public Header getContentType() { return new BasicHeader("Content-Type", contentStream[0].getContentType()); } @Override public boolean isRepeatable() { return false; } }); } else { post.setEntity(new InputStreamEntity(contentStream[0].getStream(), -1) { @Override public Header getContentType() { return new BasicHeader("Content-Type", contentStream[0].getContentType()); } @Override public boolean isRepeatable() { return false; } }); } method = post; } } else { throw new SolrServerException("Unsupported method: " + request.getMethod()); } } catch (NoHttpResponseException r) { method = null; if (is != null) { is.close(); } // If out of tries then just rethrow (as normal error). if (tries < 1) { throw r; } } } } catch (IOException ex) { throw new SolrServerException("error reading streams", ex); } // client already has this set, is this needed method.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS, followRedirects); method.addHeader("User-Agent", AGENT); // add jauth information //String username = WebServiceLoaderUtils.getWebServiceProperty(Constants.SECURITY_USERNAME).toString(); //String password = WebServiceLoaderUtils.getWebServiceProperty(Constants.SECURITY_PASSWORD).toString(); ResourceBundle bundle = ResourceBundle.getBundle("solrconfig"); String username; String password; username = bundle.getString("wsUsername.url"); password = bundle.getString("wsPassword.url"); method.addHeader("username", username); method.addHeader("password", password); InputStream respBody = null; boolean shouldClose = true; boolean success = false; try { // Execute the method. final HttpResponse response = httpClient.execute(method); int httpStatus = response.getStatusLine().getStatusCode(); // Read the contents respBody = response.getEntity().getContent(); Header ctHeader = response.getLastHeader("content-type"); String contentType; if (ctHeader != null) { contentType = ctHeader.getValue(); } else { contentType = ""; } // handle some http level checks before trying to parse the response switch (httpStatus) { case HttpStatus.SC_OK: case HttpStatus.SC_BAD_REQUEST: case HttpStatus.SC_CONFLICT: // 409 break; case HttpStatus.SC_MOVED_PERMANENTLY: case HttpStatus.SC_MOVED_TEMPORARILY: if (!followRedirects) { throw new SolrServerException( "Server at " + getBaseURL() + " sent back a redirect (" + httpStatus + ")."); } break; default: if (processor == null) { throw new RemoteSolrException(httpStatus, "Server at " + getBaseURL() + " returned non ok status:" + httpStatus + ", message:" + response.getStatusLine().getReasonPhrase(), null); } } if (processor == null) { // no processor specified, return raw stream NamedList<Object> rsp = new NamedList<Object>(); rsp.add("stream", respBody); // Only case where stream should not be closed shouldClose = false; success = true; return rsp; } String procCt = processor.getContentType(); if (procCt != null) { String procMimeType = ContentType.parse(procCt).getMimeType().trim().toLowerCase(Locale.ROOT); String mimeType = ContentType.parse(contentType).getMimeType().trim().toLowerCase(Locale.ROOT); if (!procMimeType.equals(mimeType)) { // unexpected mime type String msg = "Expected mime type " + procMimeType + " but got " + mimeType + "."; Header encodingHeader = response.getEntity().getContentEncoding(); String encoding; if (encodingHeader != null) { encoding = encodingHeader.getValue(); } else { encoding = "UTF-8"; // try UTF-8 } try { msg = msg + " " + IOUtils.toString(respBody, encoding); } catch (IOException e) { throw new RemoteSolrException(httpStatus, "Could not parse response with encoding " + encoding, e); } RemoteSolrException e = new RemoteSolrException(httpStatus, msg, null); throw e; } } // if(true) { // ByteArrayOutputStream copy = new ByteArrayOutputStream(); // IOUtils.copy(respBody, copy); // String val = new String(copy.toByteArray()); // System.out.println(">RESPONSE>"+val+"<"+val.length()); // respBody = new ByteArrayInputStream(copy.toByteArray()); // } NamedList<Object> rsp = null; String charset = EntityUtils.getContentCharSet(response.getEntity()); try { rsp = processor.processResponse(respBody, charset); } catch (Exception e) { throw new RemoteSolrException(httpStatus, e.getMessage(), e); } if (httpStatus != HttpStatus.SC_OK) { String reason = null; try { NamedList<?> err = (NamedList<?>) rsp.get("error"); if (err != null) { reason = (String) err.get("msg"); // TODO? get the trace? } } catch (Exception ex) { } if (reason == null) { StringBuilder msg = new StringBuilder(); msg.append(response.getStatusLine().getReasonPhrase()); msg.append("\n\n"); msg.append("request: " + method.getURI()); reason = java.net.URLDecoder.decode(msg.toString(), UTF_8); } throw new RemoteSolrException(httpStatus, reason, null); } success = true; return rsp; } catch (ConnectException e) { throw new SolrServerException("Server refused connection at: " + getBaseURL(), e); } catch (SocketTimeoutException e) { throw new SolrServerException("Timeout occured while waiting response from server at: " + getBaseURL(), e); } catch (IOException e) { throw new SolrServerException("IOException occured when talking to server at: " + getBaseURL(), e); } finally { if (respBody != null && shouldClose) { try { respBody.close(); } catch (IOException e) { //log.error("", e); } finally { if (!success) { method.abort(); } } } } }
From source file:lucee.runtime.tag.Http4.java
private void _doEndTag(Struct cfhttp) throws PageException, IOException { BasicHttpParams params = new BasicHttpParams(); DefaultHttpClient client = HTTPEngine4Impl.createClient(params, redirect ? HTTPEngine.MAX_REDIRECT : 0); ConfigWeb cw = pageContext.getConfig(); HttpRequestBase req = null; HttpContext httpContext = null; //HttpRequestBase req = init(pageContext.getConfig(),this,client,params,url,port); {/*from www . j a va 2s.c o m*/ if (StringUtil.isEmpty(charset, true)) charset = ((PageContextImpl) pageContext).getWebCharset().name(); else charset = charset.trim(); // check if has fileUploads boolean doUploadFile = false; for (int i = 0; i < this.params.size(); i++) { if ((this.params.get(i)).getType().equalsIgnoreCase("file")) { doUploadFile = true; break; } } // parse url (also query string) int len = this.params.size(); StringBuilder sbQS = new StringBuilder(); for (int i = 0; i < len; i++) { HttpParamBean param = this.params.get(i); String type = param.getType(); // URL if (type.equals("url")) { if (sbQS.length() > 0) sbQS.append('&'); sbQS.append(param.getEncoded() ? HttpImpl.urlenc(param.getName(), charset) : param.getName()); sbQS.append('='); sbQS.append(param.getEncoded() ? HttpImpl.urlenc(param.getValueAsString(), charset) : param.getValueAsString()); } } String host = null; HttpHost httpHost; try { URL _url = HTTPUtil.toURL(url, port, encoded); httpHost = new HttpHost(_url.getHost(), _url.getPort()); host = _url.getHost(); url = _url.toExternalForm(); if (sbQS.length() > 0) { // no existing QS if (StringUtil.isEmpty(_url.getQuery())) { url += "?" + sbQS; } else { url += "&" + sbQS; } } } catch (MalformedURLException mue) { throw Caster.toPageException(mue); } // select best matching method (get,post, post multpart (file)) boolean isBinary = false; boolean doMultiPart = doUploadFile || this.multiPart; HttpPost post = null; HttpEntityEnclosingRequest eem = null; if (this.method == METHOD_GET) { req = new HttpGet(url); } else if (this.method == METHOD_HEAD) { req = new HttpHead(url); } else if (this.method == METHOD_DELETE) { isBinary = true; req = new HttpDelete(url); } else if (this.method == METHOD_PUT) { isBinary = true; HttpPut put = new HttpPut(url); req = put; eem = put; } else if (this.method == METHOD_TRACE) { isBinary = true; req = new HttpTrace(url); } else if (this.method == METHOD_OPTIONS) { isBinary = true; req = new HttpOptions(url); } else if (this.method == METHOD_PATCH) { isBinary = true; eem = HTTPPatchFactory.getHTTPPatch(url); req = (HttpRequestBase) eem; } else { isBinary = true; post = new HttpPost(url); req = post; eem = post; } boolean hasForm = false; boolean hasBody = false; boolean hasContentType = false; // Set http params ArrayList<FormBodyPart> parts = new ArrayList<FormBodyPart>(); StringBuilder acceptEncoding = new StringBuilder(); java.util.List<NameValuePair> postParam = post != null ? new ArrayList<NameValuePair>() : null; for (int i = 0; i < len; i++) { HttpParamBean param = this.params.get(i); String type = param.getType(); // URL if (type.equals("url")) { //listQS.add(new BasicNameValuePair(translateEncoding(param.getName(), http.charset),translateEncoding(param.getValueAsString(), http.charset))); } // Form else if (type.equals("formfield") || type.equals("form")) { hasForm = true; if (this.method == METHOD_GET) throw new ApplicationException( "httpparam with type formfield can only be used when the method attribute of the parent http tag is set to post"); if (post != null) { if (doMultiPart) { parts.add(new FormBodyPart(param.getName(), new StringBody(param.getValueAsString(), CharsetUtil.toCharset(charset)))); } else { postParam.add(new BasicNameValuePair(param.getName(), param.getValueAsString())); } } //else if(multi!=null)multi.addParameter(param.getName(),param.getValueAsString()); } // CGI else if (type.equals("cgi")) { if (param.getEncoded()) req.addHeader(HttpImpl.urlenc(param.getName(), charset), HttpImpl.urlenc(param.getValueAsString(), charset)); else req.addHeader(param.getName(), param.getValueAsString()); } // Header else if (type.startsWith("head")) { if (param.getName().equalsIgnoreCase("content-type")) hasContentType = true; if (param.getName().equalsIgnoreCase("Content-Length")) { } else if (param.getName().equalsIgnoreCase("Accept-Encoding")) { acceptEncoding.append(HttpImpl.headerValue(param.getValueAsString())); acceptEncoding.append(", "); } else req.addHeader(param.getName(), HttpImpl.headerValue(param.getValueAsString())); } // Cookie else if (type.equals("cookie")) { HTTPEngine4Impl.addCookie(client, host, param.getName(), param.getValueAsString(), "/", charset); } // File else if (type.equals("file")) { hasForm = true; if (this.method == METHOD_GET) throw new ApplicationException( "httpparam type file can't only be used, when method of the tag http equal post"); String strCT = HttpImpl.getContentType(param); ContentType ct = HTTPUtil.toContentType(strCT, null); String mt = "text/xml"; if (ct != null && !StringUtil.isEmpty(ct.getMimeType(), true)) mt = ct.getMimeType(); String cs = charset; if (ct != null && !StringUtil.isEmpty(ct.getCharset(), true)) cs = ct.getCharset(); if (doMultiPart) { try { Resource res = param.getFile(); parts.add(new FormBodyPart(param.getName(), new ResourceBody(res, mt, res.getName(), cs))); //parts.add(new ResourcePart(param.getName(),new ResourcePartSource(param.getFile()),getContentType(param),_charset)); } catch (FileNotFoundException e) { throw new ApplicationException("can't upload file, path is invalid", e.getMessage()); } } } // XML else if (type.equals("xml")) { ContentType ct = HTTPUtil.toContentType(param.getMimeType(), null); String mt = "text/xml"; if (ct != null && !StringUtil.isEmpty(ct.getMimeType(), true)) mt = ct.getMimeType(); String cs = charset; if (ct != null && !StringUtil.isEmpty(ct.getCharset(), true)) cs = ct.getCharset(); hasBody = true; hasContentType = true; req.addHeader("Content-type", mt + "; charset=" + cs); if (eem == null) throw new ApplicationException("type xml is only supported for type post and put"); HTTPEngine4Impl.setBody(eem, param.getValueAsString(), mt, cs); } // Body else if (type.equals("body")) { ContentType ct = HTTPUtil.toContentType(param.getMimeType(), null); String mt = null; if (ct != null && !StringUtil.isEmpty(ct.getMimeType(), true)) mt = ct.getMimeType(); String cs = charset; if (ct != null && !StringUtil.isEmpty(ct.getCharset(), true)) cs = ct.getCharset(); hasBody = true; if (eem == null) throw new ApplicationException("type body is only supported for type post and put"); HTTPEngine4Impl.setBody(eem, param.getValue(), mt, cs); } else { throw new ApplicationException("invalid type [" + type + "]"); } } // post params if (postParam != null && postParam.size() > 0) post.setEntity(new org.apache.http.client.entity.UrlEncodedFormEntity(postParam, charset)); if (compression) { acceptEncoding.append("gzip"); } else { acceptEncoding.append("deflate;q=0"); req.setHeader("TE", "deflate;q=0"); } req.setHeader("Accept-Encoding", acceptEncoding.toString()); // multipart if (doMultiPart && eem != null) { hasContentType = true; boolean doIt = true; if (!this.multiPart && parts.size() == 1) { ContentBody body = parts.get(0).getBody(); if (body instanceof StringBody) { StringBody sb = (StringBody) body; try { org.apache.http.entity.ContentType ct = org.apache.http.entity.ContentType .create(sb.getMimeType(), sb.getCharset()); String str = IOUtil.toString(sb.getReader()); StringEntity entity = new StringEntity(str, ct); eem.setEntity(entity); } catch (IOException e) { throw Caster.toPageException(e); } doIt = false; } } if (doIt) { MultipartEntity mpe = new MultipartEntity(HttpMultipartMode.STRICT); Iterator<FormBodyPart> it = parts.iterator(); while (it.hasNext()) { FormBodyPart part = it.next(); mpe.addPart(part.getName(), part.getBody()); } eem.setEntity(mpe); } //eem.setRequestEntity(new MultipartRequestEntityFlex(parts.toArray(new Part[parts.size()]), eem.getParams(),http.multiPartType)); } if (hasBody && hasForm) throw new ApplicationException("mixing httpparam type file/formfield and body/XML is not allowed"); if (!hasContentType) { if (isBinary) { if (hasBody) req.addHeader("Content-type", "application/octet-stream"); else req.addHeader("Content-type", "application/x-www-form-urlencoded; charset=" + charset); } else { if (hasBody) req.addHeader("Content-type", "text/html; charset=" + charset); } } // set User Agent if (!HttpImpl.hasHeaderIgnoreCase(req, "User-Agent")) req.setHeader("User-Agent", this.useragent); // set timeout if (this.timeout > 0L) HTTPEngine4Impl.setTimeout(params, (int) this.timeout); // set Username and Password if (this.username != null) { if (this.password == null) this.password = ""; if (AUTH_TYPE_NTLM == this.authType) { if (StringUtil.isEmpty(this.workStation, true)) throw new ApplicationException( "attribute workstation is required when authentication type is [NTLM]"); if (StringUtil.isEmpty(this.domain, true)) throw new ApplicationException( "attribute domain is required when authentication type is [NTLM]"); HTTPEngine4Impl.setNTCredentials(client, this.username, this.password, this.workStation, this.domain); } else httpContext = HTTPEngine4Impl.setCredentials(client, httpHost, this.username, this.password, preauth); } // set Proxy ProxyData proxy = null; if (!StringUtil.isEmpty(this.proxyserver)) { proxy = ProxyDataImpl.getInstance(this.proxyserver, this.proxyport, this.proxyuser, this.proxypassword); } if (pageContext.getConfig().isProxyEnableFor(host)) { proxy = pageContext.getConfig().getProxyData(); } HTTPEngine4Impl.setProxy(client, req, proxy); } try { if (httpContext == null) httpContext = new BasicHttpContext(); /////////////////////////////////////////// EXECUTE ///////////////////////////////////////////////// Executor4 e = new Executor4(this, client, httpContext, req, redirect); HTTPResponse4Impl rsp = null; if (timeout < 0) { try { rsp = e.execute(httpContext); } catch (Throwable t) { if (!throwonerror) { setUnknownHost(cfhttp, t); return; } throw toPageException(t); } } else { e.start(); try { synchronized (this) {//print.err(timeout); this.wait(timeout); } } catch (InterruptedException ie) { throw Caster.toPageException(ie); } if (e.t != null) { if (!throwonerror) { setUnknownHost(cfhttp, e.t); return; } throw toPageException(e.t); } rsp = e.response; if (!e.done) { req.abort(); if (throwonerror) throw new HTTPException("408 Request Time-out", "a timeout occurred in tag http", 408, "Time-out", rsp.getURL()); setRequestTimeout(cfhttp); return; //throw new ApplicationException("timeout"); } } /////////////////////////////////////////// EXECUTE ///////////////////////////////////////////////// Charset responseCharset = CharsetUtil.toCharset(rsp.getCharset()); // Write Response Scope //String rawHeader=httpMethod.getStatusLine().toString(); String mimetype = null; String contentEncoding = null; // status code cfhttp.set(STATUSCODE, ((rsp.getStatusCode() + " " + rsp.getStatusText()).trim())); cfhttp.set(STATUS_CODE, new Double(rsp.getStatusCode())); cfhttp.set(STATUS_TEXT, (rsp.getStatusText())); cfhttp.set(HTTP_VERSION, (rsp.getProtocolVersion())); //responseHeader lucee.commons.net.http.Header[] headers = rsp.getAllHeaders(); StringBuffer raw = new StringBuffer(rsp.getStatusLine() + " "); Struct responseHeader = new StructImpl(); Struct cookie; Array setCookie = new ArrayImpl(); Query cookies = new QueryImpl( new String[] { "name", "value", "path", "domain", "expires", "secure", "httpOnly" }, 0, "cookies"); for (int i = 0; i < headers.length; i++) { lucee.commons.net.http.Header header = headers[i]; //print.ln(header); raw.append(header.toString() + " "); if (header.getName().equalsIgnoreCase("Set-Cookie")) { setCookie.append(header.getValue()); parseCookie(cookies, header.getValue()); } else { //print.ln(header.getName()+"-"+header.getValue()); Object value = responseHeader.get(KeyImpl.getInstance(header.getName()), null); if (value == null) responseHeader.set(KeyImpl.getInstance(header.getName()), header.getValue()); else { Array arr = null; if (value instanceof Array) { arr = (Array) value; } else { arr = new ArrayImpl(); responseHeader.set(KeyImpl.getInstance(header.getName()), arr); arr.appendEL(value); } arr.appendEL(header.getValue()); } } // Content-Type if (header.getName().equalsIgnoreCase("Content-Type")) { mimetype = header.getValue(); if (mimetype == null) mimetype = NO_MIMETYPE; } // Content-Encoding if (header.getName().equalsIgnoreCase("Content-Encoding")) { contentEncoding = header.getValue(); } } cfhttp.set(RESPONSEHEADER, responseHeader); cfhttp.set(KeyConstants._cookies, cookies); responseHeader.set(STATUS_CODE, new Double(rsp.getStatusCode())); responseHeader.set(EXPLANATION, (rsp.getStatusText())); if (setCookie.size() > 0) responseHeader.set(SET_COOKIE, setCookie); // is text boolean isText = mimetype == null || mimetype == NO_MIMETYPE || HTTPUtil.isTextMimeType(mimetype); // is multipart boolean isMultipart = MultiPartResponseUtils.isMultipart(mimetype); cfhttp.set(KeyConstants._text, Caster.toBoolean(isText)); // mimetype charset //boolean responseProvideCharset=false; if (!StringUtil.isEmpty(mimetype, true)) { if (isText) { String[] types = HTTPUtil.splitMimeTypeAndCharset(mimetype, null); if (types[0] != null) cfhttp.set(KeyConstants._mimetype, types[0]); if (types[1] != null) cfhttp.set(CHARSET, types[1]); } else cfhttp.set(KeyConstants._mimetype, mimetype); } else cfhttp.set(KeyConstants._mimetype, NO_MIMETYPE); // File Resource file = null; if (strFile != null && strPath != null) { file = ResourceUtil.toResourceNotExisting(pageContext, strPath).getRealResource(strFile); } else if (strFile != null) { file = ResourceUtil.toResourceNotExisting(pageContext, strFile); } else if (strPath != null) { file = ResourceUtil.toResourceNotExisting(pageContext, strPath); //Resource dir = file.getParentResource(); if (file.isDirectory()) { file = file.getRealResource(req.getURI().getPath());// TODO was getName() ->http://hc.apache.org/httpclient-3.x/apidocs/org/apache/commons/httpclient/URI.html#getName() } } if (file != null) pageContext.getConfig().getSecurityManager().checkFileLocation(file); // filecontent InputStream is = null; if (isText && getAsBinary != GET_AS_BINARY_YES) { String str; try { // read content if (method != METHOD_HEAD) { is = rsp.getContentAsStream(); if (is != null && HttpImpl.isGzipEncoded(contentEncoding)) is = rsp.getStatusCode() != 200 ? new CachingGZIPInputStream(is) : new GZIPInputStream(is); } try { try { str = is == null ? "" : IOUtil.toString(is, responseCharset); } catch (EOFException eof) { if (is instanceof CachingGZIPInputStream) { str = IOUtil.toString(is = ((CachingGZIPInputStream) is).getRawData(), responseCharset); } else throw eof; } } catch (UnsupportedEncodingException uee) { str = IOUtil.toString(is, (Charset) null); } } catch (IOException ioe) { throw Caster.toPageException(ioe); } finally { IOUtil.closeEL(is); } if (str == null) str = ""; if (resolveurl) { //if(e.redirectURL!=null)url=e.redirectURL.toExternalForm(); str = new URLResolver().transform(str, e.response.getTargetURL(), false); } cfhttp.set(FILE_CONTENT, str); try { if (file != null) { IOUtil.write(file, str, ((PageContextImpl) pageContext).getWebCharset(), false); } } catch (IOException e1) { } if (name != null) { Query qry = CSVParser.toQuery(str, delimiter, textqualifier, columns, firstrowasheaders); pageContext.setVariable(name, qry); } } // Binary else { byte[] barr = null; if (HttpImpl.isGzipEncoded(contentEncoding)) { if (method != METHOD_HEAD) { is = rsp.getContentAsStream(); is = rsp.getStatusCode() != 200 ? new CachingGZIPInputStream(is) : new GZIPInputStream(is); } try { try { barr = is == null ? new byte[0] : IOUtil.toBytes(is); } catch (EOFException eof) { if (is instanceof CachingGZIPInputStream) barr = IOUtil.toBytes(((CachingGZIPInputStream) is).getRawData()); else throw eof; } } catch (IOException t) { throw Caster.toPageException(t); } finally { IOUtil.closeEL(is); } } else { try { if (method != METHOD_HEAD) barr = rsp.getContentAsByteArray(); else barr = new byte[0]; } catch (IOException t) { throw Caster.toPageException(t); } } //IF Multipart response get file content and parse parts if (barr != null) { if (isMultipart) { cfhttp.set(FILE_CONTENT, MultiPartResponseUtils.getParts(barr, mimetype)); } else { cfhttp.set(FILE_CONTENT, barr); } } else cfhttp.set(FILE_CONTENT, ""); if (file != null) { try { if (barr != null) IOUtil.copy(new ByteArrayInputStream(barr), file, true); } catch (IOException ioe) { throw Caster.toPageException(ioe); } } } // header cfhttp.set(KeyConstants._header, raw.toString()); if (!HttpImpl.isStatusOK(rsp.getStatusCode())) { String msg = rsp.getStatusCode() + " " + rsp.getStatusText(); cfhttp.setEL(ERROR_DETAIL, msg); if (throwonerror) { throw new HTTPException(msg, null, rsp.getStatusCode(), rsp.getStatusText(), rsp.getURL()); } } } finally { //rsp.release(); } }
From source file:lucee.runtime.tag.Http41.java
private void _doEndTag(Struct cfhttp) throws PageException, IOException { HttpClientBuilder builder = HttpClients.custom(); // redirect//from w w w . j a v a2 s.com if (redirect) builder.setRedirectStrategy(new DefaultRedirectStrategy()); else builder.disableRedirectHandling(); // cookies BasicCookieStore cookieStore = new BasicCookieStore(); builder.setDefaultCookieStore(cookieStore); ConfigWeb cw = pageContext.getConfig(); HttpRequestBase req = null; HttpContext httpContext = null; //HttpRequestBase req = init(pageContext.getConfig(),this,client,params,url,port); { if (StringUtil.isEmpty(charset, true)) charset = ((PageContextImpl) pageContext).getWebCharset().name(); else charset = charset.trim(); // check if has fileUploads boolean doUploadFile = false; for (int i = 0; i < this.params.size(); i++) { if ((this.params.get(i)).getType().equalsIgnoreCase("file")) { doUploadFile = true; break; } } // parse url (also query string) int len = this.params.size(); StringBuilder sbQS = new StringBuilder(); for (int i = 0; i < len; i++) { HttpParamBean param = this.params.get(i); String type = param.getType(); // URL if (type.equals("url")) { if (sbQS.length() > 0) sbQS.append('&'); sbQS.append(param.getEncoded() ? HttpImpl.urlenc(param.getName(), charset) : param.getName()); sbQS.append('='); sbQS.append(param.getEncoded() ? HttpImpl.urlenc(param.getValueAsString(), charset) : param.getValueAsString()); } } String host = null; HttpHost httpHost; try { URL _url = HTTPUtil.toURL(url, port, encoded); httpHost = new HttpHost(_url.getHost(), _url.getPort()); host = _url.getHost(); url = _url.toExternalForm(); if (sbQS.length() > 0) { // no existing QS if (StringUtil.isEmpty(_url.getQuery())) { url += "?" + sbQS; } else { url += "&" + sbQS; } } } catch (MalformedURLException mue) { throw Caster.toPageException(mue); } // select best matching method (get,post, post multpart (file)) boolean isBinary = false; boolean doMultiPart = doUploadFile || this.multiPart; HttpPost post = null; HttpEntityEnclosingRequest eem = null; if (this.method == METHOD_GET) { req = new HttpGet(url); } else if (this.method == METHOD_HEAD) { req = new HttpHead(url); } else if (this.method == METHOD_DELETE) { isBinary = true; req = new HttpDelete(url); } else if (this.method == METHOD_PUT) { isBinary = true; HttpPut put = new HttpPut(url); req = put; eem = put; } else if (this.method == METHOD_TRACE) { isBinary = true; req = new HttpTrace(url); } else if (this.method == METHOD_OPTIONS) { isBinary = true; req = new HttpOptions(url); } else if (this.method == METHOD_PATCH) { isBinary = true; eem = HTTPPatchFactory.getHTTPPatch(url); req = (HttpRequestBase) eem; } else { isBinary = true; post = new HttpPost(url); req = post; eem = post; } boolean hasForm = false; boolean hasBody = false; boolean hasContentType = false; // Set http params ArrayList<FormBodyPart> parts = new ArrayList<FormBodyPart>(); StringBuilder acceptEncoding = new StringBuilder(); java.util.List<NameValuePair> postParam = post != null ? new ArrayList<NameValuePair>() : null; for (int i = 0; i < len; i++) { HttpParamBean param = this.params.get(i); String type = param.getType(); // URL if (type.equals("url")) { //listQS.add(new BasicNameValuePair(translateEncoding(param.getName(), http.charset),translateEncoding(param.getValueAsString(), http.charset))); } // Form else if (type.equals("formfield") || type.equals("form")) { hasForm = true; if (this.method == METHOD_GET) throw new ApplicationException( "httpparam with type formfield can only be used when the method attribute of the parent http tag is set to post"); if (post != null) { if (doMultiPart) { parts.add(new FormBodyPart(param.getName(), new StringBody(param.getValueAsString(), CharsetUtil.toCharset(charset)))); } else { postParam.add(new BasicNameValuePair(param.getName(), param.getValueAsString())); } } //else if(multi!=null)multi.addParameter(param.getName(),param.getValueAsString()); } // CGI else if (type.equals("cgi")) { if (param.getEncoded()) req.addHeader(HttpImpl.urlenc(param.getName(), charset), HttpImpl.urlenc(param.getValueAsString(), charset)); else req.addHeader(param.getName(), param.getValueAsString()); } // Header else if (type.startsWith("head")) { if (param.getName().equalsIgnoreCase("content-type")) hasContentType = true; if (param.getName().equalsIgnoreCase("Content-Length")) { } else if (param.getName().equalsIgnoreCase("Accept-Encoding")) { acceptEncoding.append(HttpImpl.headerValue(param.getValueAsString())); acceptEncoding.append(", "); } else req.addHeader(param.getName(), HttpImpl.headerValue(param.getValueAsString())); } // Cookie else if (type.equals("cookie")) { HTTPEngineImpl.addCookie(cookieStore, host, param.getName(), param.getValueAsString(), "/", charset); } // File else if (type.equals("file")) { hasForm = true; if (this.method == METHOD_GET) throw new ApplicationException( "httpparam type file can't only be used, when method of the tag http equal post"); String strCT = HttpImpl.getContentType(param); ContentType ct = HTTPUtil.toContentType(strCT, null); String mt = "text/xml"; if (ct != null && !StringUtil.isEmpty(ct.getMimeType(), true)) mt = ct.getMimeType(); String cs = charset; if (ct != null && !StringUtil.isEmpty(ct.getCharset(), true)) cs = ct.getCharset(); if (doMultiPart) { try { Resource res = param.getFile(); parts.add(new FormBodyPart(param.getName(), new ResourceBody(res, mt, res.getName(), cs))); //parts.add(new ResourcePart(param.getName(),new ResourcePartSource(param.getFile()),getContentType(param),_charset)); } catch (FileNotFoundException e) { throw new ApplicationException("can't upload file, path is invalid", e.getMessage()); } } } // XML else if (type.equals("xml")) { ContentType ct = HTTPUtil.toContentType(param.getMimeType(), null); String mt = "text/xml"; if (ct != null && !StringUtil.isEmpty(ct.getMimeType(), true)) mt = ct.getMimeType(); String cs = charset; if (ct != null && !StringUtil.isEmpty(ct.getCharset(), true)) cs = ct.getCharset(); hasBody = true; hasContentType = true; req.addHeader("Content-type", mt + "; charset=" + cs); if (eem == null) throw new ApplicationException("type xml is only supported for type post and put"); HTTPEngineImpl.setBody(eem, param.getValueAsString(), mt, cs); } // Body else if (type.equals("body")) { ContentType ct = HTTPUtil.toContentType(param.getMimeType(), null); String mt = null; if (ct != null && !StringUtil.isEmpty(ct.getMimeType(), true)) mt = ct.getMimeType(); String cs = charset; if (ct != null && !StringUtil.isEmpty(ct.getCharset(), true)) cs = ct.getCharset(); hasBody = true; if (eem == null) throw new ApplicationException("type body is only supported for type post and put"); HTTPEngineImpl.setBody(eem, param.getValue(), mt, cs); } else { throw new ApplicationException("invalid type [" + type + "]"); } } // post params if (postParam != null && postParam.size() > 0) post.setEntity(new org.apache.http.client.entity.UrlEncodedFormEntity(postParam, charset)); if (compression) { acceptEncoding.append("gzip"); } else { acceptEncoding.append("deflate;q=0"); req.setHeader("TE", "deflate;q=0"); } req.setHeader("Accept-Encoding", acceptEncoding.toString()); // multipart if (doMultiPart && eem != null) { hasContentType = true; boolean doIt = true; if (!this.multiPart && parts.size() == 1) { ContentBody body = parts.get(0).getBody(); if (body instanceof StringBody) { StringBody sb = (StringBody) body; try { org.apache.http.entity.ContentType ct = org.apache.http.entity.ContentType .create(sb.getMimeType(), sb.getCharset()); String str = IOUtil.toString(sb.getReader()); StringEntity entity = new StringEntity(str, ct); eem.setEntity(entity); } catch (IOException e) { throw Caster.toPageException(e); } doIt = false; } } if (doIt) { MultipartEntity mpe = new MultipartEntity(HttpMultipartMode.STRICT); Iterator<FormBodyPart> it = parts.iterator(); while (it.hasNext()) { FormBodyPart part = it.next(); mpe.addPart(part.getName(), part.getBody()); } eem.setEntity(mpe); } //eem.setRequestEntity(new MultipartRequestEntityFlex(parts.toArray(new Part[parts.size()]), eem.getParams(),http.multiPartType)); } if (hasBody && hasForm) throw new ApplicationException("mixing httpparam type file/formfield and body/XML is not allowed"); if (!hasContentType) { if (isBinary) { if (hasBody) req.addHeader("Content-type", "application/octet-stream"); else req.addHeader("Content-type", "application/x-www-form-urlencoded; charset=" + charset); } else { if (hasBody) req.addHeader("Content-type", "text/html; charset=" + charset); } } // set User Agent if (!HttpImpl.hasHeaderIgnoreCase(req, "User-Agent")) req.setHeader("User-Agent", this.useragent); // set timeout if (this.timeout > 0L) { builder.setConnectionTimeToLive(this.timeout, TimeUnit.MILLISECONDS); } // set Username and Password if (this.username != null) { if (this.password == null) this.password = ""; if (AUTH_TYPE_NTLM == this.authType) { if (StringUtil.isEmpty(this.workStation, true)) throw new ApplicationException( "attribute workstation is required when authentication type is [NTLM]"); if (StringUtil.isEmpty(this.domain, true)) throw new ApplicationException( "attribute domain is required when authentication type is [NTLM]"); HTTPEngineImpl.setNTCredentials(builder, this.username, this.password, this.workStation, this.domain); } else httpContext = HTTPEngineImpl.setCredentials(builder, httpHost, this.username, this.password, preauth); } // set Proxy ProxyData proxy = null; if (!StringUtil.isEmpty(this.proxyserver)) { proxy = ProxyDataImpl.getInstance(this.proxyserver, this.proxyport, this.proxyuser, this.proxypassword); } if (pageContext.getConfig().isProxyEnableFor(host)) { proxy = pageContext.getConfig().getProxyData(); } HTTPEngineImpl.setProxy(builder, req, proxy); } CloseableHttpClient client = null; try { if (httpContext == null) httpContext = new BasicHttpContext(); /////////////////////////////////////////// EXECUTE ///////////////////////////////////////////////// client = builder.build(); Executor41 e = new Executor41(this, client, httpContext, req, redirect); HTTPResponse4Impl rsp = null; if (timeout < 0) { try { rsp = e.execute(httpContext); } catch (Throwable t) { if (!throwonerror) { setUnknownHost(cfhttp, t); return; } throw toPageException(t); } } else { e.start(); try { synchronized (this) {//print.err(timeout); this.wait(timeout); } } catch (InterruptedException ie) { throw Caster.toPageException(ie); } if (e.t != null) { if (!throwonerror) { setUnknownHost(cfhttp, e.t); return; } throw toPageException(e.t); } rsp = e.response; if (!e.done) { req.abort(); if (throwonerror) throw new HTTPException("408 Request Time-out", "a timeout occurred in tag http", 408, "Time-out", rsp.getURL()); setRequestTimeout(cfhttp); return; //throw new ApplicationException("timeout"); } } /////////////////////////////////////////// EXECUTE ///////////////////////////////////////////////// Charset responseCharset = CharsetUtil.toCharset(rsp.getCharset()); // Write Response Scope //String rawHeader=httpMethod.getStatusLine().toString(); String mimetype = null; String contentEncoding = null; // status code cfhttp.set(STATUSCODE, ((rsp.getStatusCode() + " " + rsp.getStatusText()).trim())); cfhttp.set(STATUS_CODE, new Double(rsp.getStatusCode())); cfhttp.set(STATUS_TEXT, (rsp.getStatusText())); cfhttp.set(HTTP_VERSION, (rsp.getProtocolVersion())); //responseHeader lucee.commons.net.http.Header[] headers = rsp.getAllHeaders(); StringBuffer raw = new StringBuffer(rsp.getStatusLine() + " "); Struct responseHeader = new StructImpl(); Struct cookie; Array setCookie = new ArrayImpl(); Query cookies = new QueryImpl( new String[] { "name", "value", "path", "domain", "expires", "secure", "httpOnly" }, 0, "cookies"); for (int i = 0; i < headers.length; i++) { lucee.commons.net.http.Header header = headers[i]; //print.ln(header); raw.append(header.toString() + " "); if (header.getName().equalsIgnoreCase("Set-Cookie")) { setCookie.append(header.getValue()); parseCookie(cookies, header.getValue()); } else { //print.ln(header.getName()+"-"+header.getValue()); Object value = responseHeader.get(KeyImpl.getInstance(header.getName()), null); if (value == null) responseHeader.set(KeyImpl.getInstance(header.getName()), header.getValue()); else { Array arr = null; if (value instanceof Array) { arr = (Array) value; } else { arr = new ArrayImpl(); responseHeader.set(KeyImpl.getInstance(header.getName()), arr); arr.appendEL(value); } arr.appendEL(header.getValue()); } } // Content-Type if (header.getName().equalsIgnoreCase("Content-Type")) { mimetype = header.getValue(); if (mimetype == null) mimetype = NO_MIMETYPE; } // Content-Encoding if (header.getName().equalsIgnoreCase("Content-Encoding")) { contentEncoding = header.getValue(); } } cfhttp.set(RESPONSEHEADER, responseHeader); cfhttp.set(KeyConstants._cookies, cookies); responseHeader.set(STATUS_CODE, new Double(rsp.getStatusCode())); responseHeader.set(EXPLANATION, (rsp.getStatusText())); if (setCookie.size() > 0) responseHeader.set(SET_COOKIE, setCookie); // is text boolean isText = mimetype == null || mimetype == NO_MIMETYPE || HTTPUtil.isTextMimeType(mimetype); // is multipart boolean isMultipart = MultiPartResponseUtils.isMultipart(mimetype); cfhttp.set(KeyConstants._text, Caster.toBoolean(isText)); // mimetype charset //boolean responseProvideCharset=false; if (!StringUtil.isEmpty(mimetype, true)) { if (isText) { String[] types = HTTPUtil.splitMimeTypeAndCharset(mimetype, null); if (types[0] != null) cfhttp.set(KeyConstants._mimetype, types[0]); if (types[1] != null) cfhttp.set(CHARSET, types[1]); } else cfhttp.set(KeyConstants._mimetype, mimetype); } else cfhttp.set(KeyConstants._mimetype, NO_MIMETYPE); // File Resource file = null; if (strFile != null && strPath != null) { file = ResourceUtil.toResourceNotExisting(pageContext, strPath).getRealResource(strFile); } else if (strFile != null) { file = ResourceUtil.toResourceNotExisting(pageContext, strFile); } else if (strPath != null) { file = ResourceUtil.toResourceNotExisting(pageContext, strPath); //Resource dir = file.getParentResource(); if (file.isDirectory()) { file = file.getRealResource(req.getURI().getPath());// TODO was getName() ->http://hc.apache.org/httpclient-3.x/apidocs/org/apache/commons/httpclient/URI.html#getName() } } if (file != null) pageContext.getConfig().getSecurityManager().checkFileLocation(file); // filecontent InputStream is = null; if (isText && getAsBinary != GET_AS_BINARY_YES) { String str; try { // read content if (method != METHOD_HEAD) { is = rsp.getContentAsStream(); if (is != null && HttpImpl.isGzipEncoded(contentEncoding)) is = rsp.getStatusCode() != 200 ? new CachingGZIPInputStream(is) : new GZIPInputStream(is); } try { try { str = is == null ? "" : IOUtil.toString(is, responseCharset); } catch (EOFException eof) { if (is instanceof CachingGZIPInputStream) { str = IOUtil.toString(is = ((CachingGZIPInputStream) is).getRawData(), responseCharset); } else throw eof; } } catch (UnsupportedEncodingException uee) { str = IOUtil.toString(is, (Charset) null); } } catch (IOException ioe) { throw Caster.toPageException(ioe); } finally { IOUtil.closeEL(is); } if (str == null) str = ""; if (resolveurl) { //if(e.redirectURL!=null)url=e.redirectURL.toExternalForm(); str = new URLResolver().transform(str, e.response.getTargetURL(), false); } cfhttp.set(FILE_CONTENT, str); try { if (file != null) { IOUtil.write(file, str, ((PageContextImpl) pageContext).getWebCharset(), false); } } catch (IOException e1) { } if (name != null) { Query qry = CSVParser.toQuery(str, delimiter, textqualifier, columns, firstrowasheaders); pageContext.setVariable(name, qry); } } // Binary else { byte[] barr = null; if (HttpImpl.isGzipEncoded(contentEncoding)) { if (method != METHOD_HEAD) { is = rsp.getContentAsStream(); is = rsp.getStatusCode() != 200 ? new CachingGZIPInputStream(is) : new GZIPInputStream(is); } try { try { barr = is == null ? new byte[0] : IOUtil.toBytes(is); } catch (EOFException eof) { if (is instanceof CachingGZIPInputStream) barr = IOUtil.toBytes(((CachingGZIPInputStream) is).getRawData()); else throw eof; } } catch (IOException t) { throw Caster.toPageException(t); } finally { IOUtil.closeEL(is); } } else { try { if (method != METHOD_HEAD) barr = rsp.getContentAsByteArray(); else barr = new byte[0]; } catch (IOException t) { throw Caster.toPageException(t); } } //IF Multipart response get file content and parse parts if (barr != null) { if (isMultipart) { cfhttp.set(FILE_CONTENT, MultiPartResponseUtils.getParts(barr, mimetype)); } else { cfhttp.set(FILE_CONTENT, barr); } } else cfhttp.set(FILE_CONTENT, ""); if (file != null) { try { if (barr != null) IOUtil.copy(new ByteArrayInputStream(barr), file, true); } catch (IOException ioe) { throw Caster.toPageException(ioe); } } } // header cfhttp.set(KeyConstants._header, raw.toString()); if (!HttpImpl.isStatusOK(rsp.getStatusCode())) { String msg = rsp.getStatusCode() + " " + rsp.getStatusText(); cfhttp.setEL(ERROR_DETAIL, msg); if (throwonerror) { throw new HTTPException(msg, null, rsp.getStatusCode(), rsp.getStatusText(), rsp.getURL()); } } } finally { if (client != null) client.close(); } }
From source file:lucee.runtime.tag.Http.java
private void _doEndTag() throws PageException, IOException { long start = System.nanoTime(); HttpClientBuilder builder = HTTPEngine4Impl.getHttpClientBuilder(); ssl(builder);//from ww w. ja v a 2s.c o m // redirect if (redirect) builder.setRedirectStrategy(new DefaultRedirectStrategy()); else builder.disableRedirectHandling(); // cookies BasicCookieStore cookieStore = new BasicCookieStore(); builder.setDefaultCookieStore(cookieStore); ConfigWeb cw = pageContext.getConfig(); HttpRequestBase req = null; HttpContext httpContext = null; CacheHandler cacheHandler = null; String cacheId = null; // HttpRequestBase req = init(pageContext.getConfig(),this,client,params,url,port); { if (StringUtil.isEmpty(charset, true)) charset = ((PageContextImpl) pageContext).getWebCharset().name(); else charset = charset.trim(); // check if has fileUploads boolean doUploadFile = false; for (int i = 0; i < this.params.size(); i++) { if ((this.params.get(i)).getType() == HttpParamBean.TYPE_FILE) { doUploadFile = true; break; } } // parse url (also query string) int len = this.params.size(); StringBuilder sbQS = new StringBuilder(); for (int i = 0; i < len; i++) { HttpParamBean param = this.params.get(i); int type = param.getType(); // URL if (type == HttpParamBean.TYPE_URL) { if (sbQS.length() > 0) sbQS.append('&'); sbQS.append(param.getEncoded() ? urlenc(param.getName(), charset) : param.getName()); sbQS.append('='); sbQS.append(param.getEncoded() ? urlenc(param.getValueAsString(), charset) : param.getValueAsString()); } } String host = null; HttpHost httpHost; try { URL _url = HTTPUtil.toURL(url, port, encoded); httpHost = new HttpHost(_url.getHost(), _url.getPort()); host = _url.getHost(); url = _url.toExternalForm(); if (sbQS.length() > 0) { // no existing QS if (StringUtil.isEmpty(_url.getQuery())) { url += "?" + sbQS; } else { url += "&" + sbQS; } } } catch (MalformedURLException mue) { throw Caster.toPageException(mue); } // cache if (cachedWithin != null) { cacheId = createCacheId(); cacheHandler = pageContext.getConfig().getCacheHandlerCollection(Config.CACHE_TYPE_HTTP, null) .getInstanceMatchingObject(cachedWithin, null); if (cacheHandler instanceof CacheHandlerPro) { CacheItem cacheItem = ((CacheHandlerPro) cacheHandler).get(pageContext, cacheId, cachedWithin); if (cacheItem instanceof HTTPCacheItem) { pageContext.setVariable(result, ((HTTPCacheItem) cacheItem).getData()); return; } } else if (cacheHandler != null) { // TODO this else block can be removed when all cache handlers implement CacheHandlerPro CacheItem cacheItem = cacheHandler.get(pageContext, cacheId); if (cacheItem instanceof HTTPCacheItem) { pageContext.setVariable(result, ((HTTPCacheItem) cacheItem).getData()); return; } } } // cache not found, process and cache result if needed // select best matching method (get,post, post multpart (file)) boolean isBinary = false; boolean doMultiPart = doUploadFile || this.multiPart; HttpEntityEnclosingRequest eeReqPost = null; HttpEntityEnclosingRequest eeReq = null; if (this.method == METHOD_GET) { req = new HttpGetWithBody(url); eeReq = (HttpEntityEnclosingRequest) req; } else if (this.method == METHOD_HEAD) { req = new HttpHead(url); } else if (this.method == METHOD_DELETE) { isBinary = true; req = new HttpDeleteWithBody(url); eeReq = (HttpEntityEnclosingRequest) req; } else if (this.method == METHOD_PUT) { isBinary = true; HttpPut put = new HttpPut(url); eeReqPost = put; req = put; eeReq = put; } else if (this.method == METHOD_TRACE) { isBinary = true; req = new HttpTrace(url); } else if (this.method == METHOD_OPTIONS) { isBinary = true; req = new HttpOptions(url); } else if (this.method == METHOD_PATCH) { isBinary = true; eeReq = HTTPPatchFactory.getHTTPPatch(url); req = (HttpRequestBase) eeReq; } else { isBinary = true; eeReqPost = new HttpPost(url); req = (HttpPost) eeReqPost; eeReq = eeReqPost; } boolean hasForm = false; boolean hasBody = false; boolean hasContentType = false; // Set http params ArrayList<FormBodyPart> parts = new ArrayList<FormBodyPart>(); StringBuilder acceptEncoding = new StringBuilder(); java.util.List<NameValuePair> postParam = eeReqPost != null ? new ArrayList<NameValuePair>() : null; for (int i = 0; i < len; i++) { HttpParamBean param = this.params.get(i); int type = param.getType(); // URL if (type == HttpParamBean.TYPE_URL) { // listQS.add(new BasicNameValuePair(translateEncoding(param.getName(), http.charset),translateEncoding(param.getValueAsString(), // http.charset))); } // Form else if (type == HttpParamBean.TYPE_FORM) { hasForm = true; if (this.method == METHOD_GET) throw new ApplicationException( "httpparam with type formfield can only be used when the method attribute of the parent http tag is set to post"); if (eeReqPost != null) { if (doMultiPart) { parts.add(new FormBodyPart(param.getName(), new StringBody(param.getValueAsString(), CharsetUtil.toCharset(charset)))); } else { postParam.add(new BasicNameValuePair(param.getName(), param.getValueAsString())); } } // else if(multi!=null)multi.addParameter(param.getName(),param.getValueAsString()); } // CGI else if (type == HttpParamBean.TYPE_CGI) { if (param.getEncoded()) req.addHeader(urlenc(param.getName(), charset), urlenc(param.getValueAsString(), charset)); else req.addHeader(param.getName(), param.getValueAsString()); } // Header else if (type == HttpParamBean.TYPE_HEADER) { if (param.getName().equalsIgnoreCase("content-type")) hasContentType = true; if (param.getName().equalsIgnoreCase("Content-Length")) { } else if (param.getName().equalsIgnoreCase("Accept-Encoding")) { acceptEncoding.append(headerValue(param.getValueAsString())); acceptEncoding.append(", "); } else req.addHeader(param.getName(), headerValue(param.getValueAsString())); } // Cookie else if (type == HttpParamBean.TYPE_COOKIE) { HTTPEngine4Impl.addCookie(cookieStore, host, param.getName(), param.getValueAsString(), "/", charset); } // File else if (type == HttpParamBean.TYPE_FILE) { hasForm = true; if (this.method == METHOD_GET) throw new ApplicationException( "httpparam type file can't only be used, when method of the tag http equal post"); // if(param.getFile()==null) throw new ApplicationException("httpparam type file can't only be used, when method of the tag http equal // post"); String strCT = getContentType(param); ContentType ct = HTTPUtil.toContentType(strCT, null); String mt = "text/xml"; if (ct != null && !StringUtil.isEmpty(ct.getMimeType(), true)) mt = ct.getMimeType(); String cs = charset; if (ct != null && !StringUtil.isEmpty(ct.getCharset(), true)) cs = ct.getCharset(); if (doMultiPart) { try { Resource res = param.getFile(); parts.add(new FormBodyPart(param.getName(), new ResourceBody(res, mt, res.getName(), cs))); // parts.add(new ResourcePart(param.getName(),new ResourcePartSource(param.getFile()),getContentType(param),_charset)); } catch (FileNotFoundException e) { throw new ApplicationException("can't upload file, path is invalid", e.getMessage()); } } } // XML else if (type == HttpParamBean.TYPE_XML) { ContentType ct = HTTPUtil.toContentType(param.getMimeType(), null); String mt = "text/xml"; if (ct != null && !StringUtil.isEmpty(ct.getMimeType(), true)) mt = ct.getMimeType(); String cs = charset; if (ct != null && !StringUtil.isEmpty(ct.getCharset(), true)) cs = ct.getCharset(); hasBody = true; hasContentType = true; req.addHeader("Content-type", mt + "; charset=" + cs); if (eeReq == null) throw new ApplicationException( "type xml is only supported for methods get, delete, post, and put"); HTTPEngine4Impl.setBody(eeReq, param.getValueAsString(), mt, cs); } // Body else if (type == HttpParamBean.TYPE_BODY) { ContentType ct = HTTPUtil.toContentType(param.getMimeType(), null); String mt = null; if (ct != null && !StringUtil.isEmpty(ct.getMimeType(), true)) mt = ct.getMimeType(); String cs = charset; if (ct != null && !StringUtil.isEmpty(ct.getCharset(), true)) cs = ct.getCharset(); hasBody = true; if (eeReq == null) throw new ApplicationException( "type body is only supported for methods get, delete, post, and put"); HTTPEngine4Impl.setBody(eeReq, param.getValue(), mt, cs); } else { throw new ApplicationException("invalid type [" + type + "]"); } } // post params if (postParam != null && postParam.size() > 0) eeReqPost.setEntity(new org.apache.http.client.entity.UrlEncodedFormEntity(postParam, charset)); if (compression) { acceptEncoding.append("gzip"); } else { acceptEncoding.append("deflate;q=0"); req.setHeader("TE", "deflate;q=0"); } req.setHeader("Accept-Encoding", acceptEncoding.toString()); // multipart if (doMultiPart && eeReq != null) { hasContentType = true; boolean doIt = true; if (!this.multiPart && parts.size() == 1) { ContentBody body = parts.get(0).getBody(); if (body instanceof StringBody) { StringBody sb = (StringBody) body; try { org.apache.http.entity.ContentType ct = org.apache.http.entity.ContentType .create(sb.getMimeType(), sb.getCharset()); String str = IOUtil.toString(sb.getReader()); StringEntity entity = new StringEntity(str, ct); eeReq.setEntity(entity); } catch (IOException e) { throw Caster.toPageException(e); } doIt = false; } } if (doIt) { MultipartEntityBuilder mpeBuilder = MultipartEntityBuilder.create().setStrictMode(); // enabling the line below will append charset=... to the Content-Type header // if (!StringUtil.isEmpty(charset, true)) // mpeBuilder.setCharset(CharsetUtil.toCharset(charset)); Iterator<FormBodyPart> it = parts.iterator(); while (it.hasNext()) { FormBodyPart part = it.next(); mpeBuilder.addPart(part); } eeReq.setEntity(mpeBuilder.build()); } // eem.setRequestEntity(new MultipartRequestEntityFlex(parts.toArray(new Part[parts.size()]), eem.getParams(),http.multiPartType)); } if (hasBody && hasForm) throw new ApplicationException("mixing httpparam type file/formfield and body/XML is not allowed"); if (!hasContentType) { if (isBinary) { if (hasBody) req.addHeader("Content-type", "application/octet-stream"); else req.addHeader("Content-type", "application/x-www-form-urlencoded; charset=" + charset); } else { if (hasBody) req.addHeader("Content-type", "text/html; charset=" + charset); } } // set User Agent if (!hasHeaderIgnoreCase(req, "User-Agent")) req.setHeader("User-Agent", this.useragent); // set timeout setTimeout(builder, checkRemainingTimeout()); // set Username and Password if (this.username != null) { if (this.password == null) this.password = ""; if (AUTH_TYPE_NTLM == this.authType) { if (StringUtil.isEmpty(this.workStation, true)) throw new ApplicationException( "attribute workstation is required when authentication type is [NTLM]"); if (StringUtil.isEmpty(this.domain, true)) throw new ApplicationException( "attribute domain is required when authentication type is [NTLM]"); HTTPEngine4Impl.setNTCredentials(builder, this.username, this.password, this.workStation, this.domain); } else httpContext = HTTPEngine4Impl.setCredentials(builder, httpHost, this.username, this.password, preauth); } // set Proxy ProxyData proxy = null; if (!StringUtil.isEmpty(this.proxyserver)) { proxy = ProxyDataImpl.getInstance(this.proxyserver, this.proxyport, this.proxyuser, this.proxypassword); } if (pageContext.getConfig().isProxyEnableFor(host)) { proxy = pageContext.getConfig().getProxyData(); } HTTPEngine4Impl.setProxy(builder, req, proxy); } CloseableHttpClient client = null; try { if (httpContext == null) httpContext = new BasicHttpContext(); Struct cfhttp = new StructImpl(); cfhttp.setEL(ERROR_DETAIL, ""); pageContext.setVariable(result, cfhttp); /////////////////////////////////////////// EXECUTE ///////////////////////////////////////////////// client = builder.build(); Executor4 e = new Executor4(pageContext, this, client, httpContext, req, redirect); HTTPResponse4Impl rsp = null; if (timeout == null || timeout.getMillis() <= 0) { try { rsp = e.execute(httpContext); } catch (Throwable t) { ExceptionUtil.rethrowIfNecessary(t); if (!throwonerror) { if (t instanceof SocketTimeoutException) setRequestTimeout(cfhttp); else setUnknownHost(cfhttp, t); return; } throw toPageException(t, rsp); } } else { e.start(); try { synchronized (this) {// print.err(timeout); this.wait(timeout.getMillis()); } } catch (InterruptedException ie) { throw Caster.toPageException(ie); } if (e.t != null) { if (!throwonerror) { setUnknownHost(cfhttp, e.t); return; } throw toPageException(e.t, rsp); } rsp = e.response; if (!e.done) { req.abort(); if (throwonerror) throw new HTTPException("408 Request Time-out", "a timeout occurred in tag http", 408, "Time-out", rsp == null ? null : rsp.getURL()); setRequestTimeout(cfhttp); return; // throw new ApplicationException("timeout"); } } /////////////////////////////////////////// EXECUTE ///////////////////////////////////////////////// Charset responseCharset = CharsetUtil.toCharset(rsp.getCharset()); int statCode = 0; // Write Response Scope // String rawHeader=httpMethod.getStatusLine().toString(); String mimetype = null; String contentEncoding = null; // status code cfhttp.set(STATUSCODE, ((rsp.getStatusCode() + " " + rsp.getStatusText()).trim())); cfhttp.set(STATUS_CODE, new Double(statCode = rsp.getStatusCode())); cfhttp.set(STATUS_TEXT, (rsp.getStatusText())); cfhttp.set(HTTP_VERSION, (rsp.getProtocolVersion())); // responseHeader lucee.commons.net.http.Header[] headers = rsp.getAllHeaders(); StringBuffer raw = new StringBuffer(rsp.getStatusLine() + " "); Struct responseHeader = new StructImpl(); Struct cookie; Array setCookie = new ArrayImpl(); Query cookies = new QueryImpl( new String[] { "name", "value", "path", "domain", "expires", "secure", "httpOnly" }, 0, "cookies"); for (int i = 0; i < headers.length; i++) { lucee.commons.net.http.Header header = headers[i]; // print.ln(header); raw.append(header.toString() + " "); if (header.getName().equalsIgnoreCase("Set-Cookie")) { setCookie.append(header.getValue()); parseCookie(cookies, header.getValue()); } else { // print.ln(header.getName()+"-"+header.getValue()); Object value = responseHeader.get(KeyImpl.getInstance(header.getName()), null); if (value == null) responseHeader.set(KeyImpl.getInstance(header.getName()), header.getValue()); else { Array arr = null; if (value instanceof Array) { arr = (Array) value; } else { arr = new ArrayImpl(); responseHeader.set(KeyImpl.getInstance(header.getName()), arr); arr.appendEL(value); } arr.appendEL(header.getValue()); } } // Content-Type if (header.getName().equalsIgnoreCase("Content-Type")) { mimetype = header.getValue(); if (mimetype == null) mimetype = NO_MIMETYPE; } // Content-Encoding if (header.getName().equalsIgnoreCase("Content-Encoding")) { contentEncoding = header.getValue(); } } cfhttp.set(RESPONSEHEADER, responseHeader); cfhttp.set(KeyConstants._cookies, cookies); responseHeader.set(STATUS_CODE, new Double(statCode = rsp.getStatusCode())); responseHeader.set(EXPLANATION, (rsp.getStatusText())); if (setCookie.size() > 0) responseHeader.set(SET_COOKIE, setCookie); // is text boolean isText = mimetype == null || mimetype == NO_MIMETYPE || HTTPUtil.isTextMimeType(mimetype); // is multipart boolean isMultipart = MultiPartResponseUtils.isMultipart(mimetype); cfhttp.set(KeyConstants._text, Caster.toBoolean(isText)); // mimetype charset // boolean responseProvideCharset=false; if (!StringUtil.isEmpty(mimetype, true)) { if (isText) { String[] types = HTTPUtil.splitMimeTypeAndCharset(mimetype, null); if (types[0] != null) cfhttp.set(KeyConstants._mimetype, types[0]); if (types[1] != null) cfhttp.set(CHARSET, types[1]); } else cfhttp.set(KeyConstants._mimetype, mimetype); } else cfhttp.set(KeyConstants._mimetype, NO_MIMETYPE); // File Resource file = null; if (strFile != null && strPath != null) { file = ResourceUtil.toResourceNotExisting(pageContext, strPath).getRealResource(strFile); } else if (strFile != null) { file = ResourceUtil.toResourceNotExisting(pageContext, strFile); } else if (strPath != null) { file = ResourceUtil.toResourceNotExisting(pageContext, strPath); // Resource dir = file.getParentResource(); if (file.isDirectory()) { file = file.getRealResource(req.getURI().getPath());// TODO was getName() // ->http://hc.apache.org/httpclient-3.x/apidocs/org/apache/commons/httpclient/URI.html#getName() } } if (file != null) pageContext.getConfig().getSecurityManager().checkFileLocation(file); // filecontent InputStream is = null; if (isText && getAsBinary != GET_AS_BINARY_YES) { String str; try { // read content if (method != METHOD_HEAD) { is = rsp.getContentAsStream(); if (is != null && isGzipEncoded(contentEncoding)) is = rsp.getStatusCode() != 200 ? new CachingGZIPInputStream(is) : new GZIPInputStream(is); } try { try { str = is == null ? "" : IOUtil.toString(is, responseCharset, checkRemainingTimeout().getMillis()); } catch (EOFException eof) { if (is instanceof CachingGZIPInputStream) { str = IOUtil.toString(is = ((CachingGZIPInputStream) is).getRawData(), responseCharset, checkRemainingTimeout().getMillis()); } else throw eof; } } catch (UnsupportedEncodingException uee) { str = IOUtil.toString(is, (Charset) null, checkRemainingTimeout().getMillis()); } } catch (IOException ioe) { throw Caster.toPageException(ioe); } finally { IOUtil.closeEL(is); } if (str == null) str = ""; if (resolveurl) { // if(e.redirectURL!=null)url=e.redirectURL.toExternalForm(); str = new URLResolver().transform(str, e.response.getTargetURL(), false); } cfhttp.set(KeyConstants._filecontent, str); try { if (file != null) { IOUtil.write(file, str, ((PageContextImpl) pageContext).getWebCharset(), false); } } catch (IOException e1) { } if (name != null) { Query qry = CSVParser.toQuery(str, delimiter, textqualifier, columns, firstrowasheaders); pageContext.setVariable(name, qry); } } // Binary else { byte[] barr = null; if (isGzipEncoded(contentEncoding)) { if (method != METHOD_HEAD) { is = rsp.getContentAsStream(); is = rsp.getStatusCode() != 200 ? new CachingGZIPInputStream(is) : new GZIPInputStream(is); } try { try { barr = is == null ? new byte[0] : IOUtil.toBytes(is); } catch (EOFException eof) { if (is instanceof CachingGZIPInputStream) barr = IOUtil.toBytes(((CachingGZIPInputStream) is).getRawData()); else throw eof; } } catch (IOException t) { throw Caster.toPageException(t); } finally { IOUtil.closeEL(is); } } else { try { if (method != METHOD_HEAD) barr = rsp.getContentAsByteArray(); else barr = new byte[0]; } catch (IOException t) { throw Caster.toPageException(t); } } // IF Multipart response get file content and parse parts if (barr != null) { if (isMultipart) { cfhttp.set(KeyConstants._filecontent, MultiPartResponseUtils.getParts(barr, mimetype)); } else { cfhttp.set(KeyConstants._filecontent, barr); } } else cfhttp.set(KeyConstants._filecontent, ""); if (file != null) { try { if (barr != null) IOUtil.copy(new ByteArrayInputStream(barr), file, true); } catch (IOException ioe) { throw Caster.toPageException(ioe); } } } // header cfhttp.set(KeyConstants._header, raw.toString()); if (!isStatusOK(rsp.getStatusCode())) { String msg = rsp.getStatusCode() + " " + rsp.getStatusText(); cfhttp.setEL(ERROR_DETAIL, msg); if (throwonerror) { throw new HTTPException(msg, null, rsp.getStatusCode(), rsp.getStatusText(), rsp.getURL()); } } // TODO: check if we can use statCode instead of rsp.getStatusCode() everywhere and cleanup the code if (cacheHandler != null && rsp.getStatusCode() == 200) { // add to cache cacheHandler.set(pageContext, cacheId, cachedWithin, new HTTPCacheItem(cfhttp, url, System.nanoTime() - start)); } } finally { if (client != null) client.close(); } }
From source file:org.apache.camel.component.http4.HttpProducer.java
public void process(Exchange exchange) throws Exception { if (getEndpoint().isBridgeEndpoint()) { exchange.setProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.TRUE); }//from w w w . j a v a2 s . c om HttpRequestBase httpRequest = createMethod(exchange); Message in = exchange.getIn(); String httpProtocolVersion = in.getHeader(Exchange.HTTP_PROTOCOL_VERSION, String.class); if (httpProtocolVersion != null) { // set the HTTP protocol version httpRequest.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpHelper.parserHttpVersion(httpProtocolVersion)); } HeaderFilterStrategy strategy = getEndpoint().getHeaderFilterStrategy(); // propagate headers as HTTP headers for (Map.Entry<String, Object> entry : in.getHeaders().entrySet()) { String headerValue = in.getHeader(entry.getKey(), String.class); if (strategy != null && !strategy.applyFilterToCamelHeaders(entry.getKey(), headerValue, exchange)) { httpRequest.addHeader(entry.getKey(), headerValue); } } // lets store the result in the output message. HttpResponse httpResponse = null; try { if (LOG.isDebugEnabled()) { LOG.debug("Executing http " + httpRequest.getMethod() + " method: " + httpRequest.getURI().toString()); } httpResponse = executeMethod(httpRequest); int responseCode = httpResponse.getStatusLine().getStatusCode(); if (LOG.isDebugEnabled()) { LOG.debug("Http responseCode: " + responseCode); } if (throwException && (responseCode < 100 || responseCode >= 300)) { throw populateHttpOperationFailedException(exchange, httpRequest, httpResponse, responseCode); } else { populateResponse(exchange, httpRequest, httpResponse, in, strategy, responseCode); } } finally { if (httpResponse != null && httpResponse.getEntity() != null) { try { httpResponse.getEntity().consumeContent(); } catch (IOException e) { // nothing we could do } } } }