📄 objecttohttpclientmethodrequest.java
字号:
PutMethod putMethod = new PutMethod(uri.toString()); setupEntityMethod(src, outputEncoding, msg, uri, putMethod); httpMethod = putMethod; } else if (HttpConstants.METHOD_DELETE.equalsIgnoreCase(method)) { httpMethod = new DeleteMethod(uri.toString()); } else if (HttpConstants.METHOD_HEAD.equalsIgnoreCase(method)) { httpMethod = new HeadMethod(uri.toString()); } else if (HttpConstants.METHOD_OPTIONS.equalsIgnoreCase(method)) { httpMethod = new OptionsMethod(uri.toString()); } else if (HttpConstants.METHOD_TRACE.equalsIgnoreCase(method)) { httpMethod = new TraceMethod(uri.toString()); } else { throw new TransformerException(HttpMessages.unsupportedMethod(method)); } // Allow the user to set HttpMethodParams as an object on the message HttpMethodParams params = (HttpMethodParams) msg.removeProperty(HttpConnector.HTTP_PARAMS_PROPERTY); if (params != null) { httpMethod.setParams(params); } else { // TODO we should probably set other properties here String httpVersion = msg.getStringProperty(HttpConnector.HTTP_VERSION_PROPERTY, HttpConstants.HTTP11); if (HttpConstants.HTTP10.equals(httpVersion)) { httpMethod.getParams().setVersion(HttpVersion.HTTP_1_0); } else { httpMethod.getParams().setVersion(HttpVersion.HTTP_1_1); } } setHeaders(httpMethod, msg); return httpMethod; } catch (Exception e) { throw new TransformerException(this, e); } } protected void setupEntityMethod(Object src, String encoding, MuleMessage msg, URI uri, EntityEnclosingMethod postMethod) throws UnsupportedEncodingException, TransformerException { // Dont set a POST payload if the body is a Null Payload. // This way client calls // can control if a POST body is posted explicitly if (!(msg.getPayload() instanceof NullPayload)) { // See if we have a MIME type set String mimeType = msg.getStringProperty(HttpConstants.HEADER_CONTENT_TYPE, null); if (src instanceof String) { // Ensure that we strip the encoding information from the // encoding type if (mimeType != null) { int parameterIndex = mimeType.indexOf(";"); if (parameterIndex > 0) { mimeType = mimeType.substring(0, parameterIndex); } } if (mimeType == null) { mimeType = HttpConstants.DEFAULT_CONTENT_TYPE; } postMethod.setRequestEntity(new StringRequestEntity(src.toString(), mimeType, encoding)); return; } if (mimeType == null) { mimeType = HttpConstants.DEFAULT_CONTENT_TYPE; } if (encoding != null && !"UTF-8".equals(encoding.toUpperCase()) && mimeType.indexOf("charset") == -1) { mimeType += "; charset=" + encoding; } if (src instanceof InputStream) { // TODO Danger here! We don't know if the content is // really text or not if (mimeType == null) { mimeType = HttpConstants.DEFAULT_CONTENT_TYPE; } postMethod.setRequestEntity(new InputStreamRequestEntity((InputStream) src, mimeType)); } else if (src instanceof byte[]) { if (mimeType == null) { mimeType = HttpConstants.DEFAULT_CONTENT_TYPE; } postMethod.setRequestEntity(new ByteArrayRequestEntity((byte[]) src, mimeType)); } else if (src instanceof OutputHandler) { MuleEvent event = RequestContext.getEvent(); postMethod.setRequestEntity(new StreamPayloadRequestEntity((OutputHandler) src, event)); } else { if (mimeType == null) { mimeType = HttpConstants.DEFAULT_CONTENT_TYPE; } byte[] buffer = SerializationUtils.serialize((Serializable) src); postMethod.setRequestEntity(new ByteArrayRequestEntity(buffer, mimeType)); } } } protected void setHeaders(HttpMethod httpMethod, MuleMessage msg) { // Standard requestHeaders String headerValue; String headerName; for (Iterator iterator = msg.getPropertyNames().iterator(); iterator.hasNext();) { headerName = (String) iterator.next(); if (headerName.equalsIgnoreCase(HttpConnector.HTTP_CUSTOM_HEADERS_MAP_PROPERTY)) { Map customHeaders = (Map) msg.getProperty(HttpConnector.HTTP_CUSTOM_HEADERS_MAP_PROPERTY); if (customHeaders != null) { for (Iterator headerItr = customHeaders.entrySet().iterator(); headerItr.hasNext();) { Map.Entry entry = (Map.Entry) headerItr.next(); if (entry.getValue() != null) { httpMethod.addRequestHeader(entry.getKey().toString(), entry.getValue().toString()); } } } } else if (HttpConstants.REQUEST_HEADER_NAMES.get(headerName) == null) { headerValue = msg.getStringProperty(headerName, null); if (headerName.startsWith(MuleProperties.PROPERTY_PREFIX)) { headerName = new StringBuffer(30).append("X-").append(headerName).toString(); } httpMethod.addRequestHeader(headerName, headerValue); } } Set attNams = msg.getAttachmentNames(); if (msg.getPayload() instanceof InputStream && attNams != null && attNams.size() > 0) { // must set this for receiver to properly parse attachments httpMethod.addRequestHeader(HttpConstants.HEADER_CONTENT_TYPE, "multipart/related"); } } protected String paramToString(Object param) { StringBuffer buf = new StringBuffer(); if (param instanceof List) { List list = (List) param; for (Iterator iterator = list.iterator(); iterator.hasNext();) { Object object = iterator.next(); buf.append(object).append(","); } return buf.toString(); } else if (param instanceof Map) { Map map = (Map) param; for (Iterator iterator = map.entrySet().iterator(); iterator.hasNext();) { Map.Entry entry = (Map.Entry) iterator.next(); buf.append(entry.getKey()).append("=").append(entry.getValue()).append("&"); } return buf.toString().substring(0, buf.length() - 1); } else { return param.toString(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -