📄 commonshttpsender.java
字号:
if (log.isDebugEnabled()) { log.debug(Messages.getMessage("exit00", "CommonsHTTPSender::invoke")); } } /** * little helper function for cookies. fills up the message context with * a string or an array of strings (if there are more than one Set-Cookie) * * @param cookieName * @param setCookieName * @param cookie * @param msgContext */ public void handleCookie(String cookieName, String cookie, MessageContext msgContext) { cookie = cleanupCookie(cookie); int keyIndex = cookie.indexOf("="); String key = (keyIndex != -1) ? cookie.substring(0, keyIndex) : cookie; ArrayList cookies = new ArrayList(); Object oldCookies = msgContext.getProperty(cookieName); boolean alreadyExist = false; if(oldCookies != null) { if(oldCookies instanceof String[]) { String[] oldCookiesArray = (String[])oldCookies; for(int i = 0; i < oldCookiesArray.length; i++) { String anOldCookie = oldCookiesArray[i]; if (key != null && anOldCookie.indexOf(key) == 0) { // same cookie key anOldCookie = cookie; // update to new one alreadyExist = true; } cookies.add(anOldCookie); } } else { String oldCookie = (String)oldCookies; if (key != null && oldCookie.indexOf(key) == 0) { // same cookie key oldCookie = cookie; // update to new one alreadyExist = true; } cookies.add(oldCookie); } } if (!alreadyExist) { cookies.add(cookie); } if(cookies.size()==1) { msgContext.setProperty(cookieName, cookies.get(0)); } else if (cookies.size() > 1) { msgContext.setProperty(cookieName, cookies.toArray(new String[cookies.size()])); } } /** * Add cookies from message context * * @param msgContext * @param state * @param header * @param host * @param path * @param secure */ private void fillHeaders(MessageContext msgContext, HttpState state, String header, String host, String path, boolean secure) { Object ck1 = msgContext.getProperty(header); if (ck1 != null) { if (ck1 instanceof String[]) { String [] cookies = (String[]) ck1; for (int i = 0; i < cookies.length; i++) { addCookie(state, cookies[i], host, path, secure); } } else { addCookie(state, (String) ck1, host, path, secure); } } } /** * add cookie to state * @param state * @param cookie */ private void addCookie(HttpState state, String cookie,String host, String path, boolean secure) { int index = cookie.indexOf('='); state.addCookie(new Cookie(host, cookie.substring(0, index), cookie.substring(index + 1), path, null, secure)); } /** * cleanup the cookie value. * * @param cookie initial cookie value * * @return a cleaned up cookie value. */ private String cleanupCookie(String cookie) { cookie = cookie.trim(); // chop after first ; a la Apache SOAP (see HTTPUtils.java there) int index = cookie.indexOf(';'); if (index != -1) { cookie = cookie.substring(0, index); } return cookie; } protected HostConfiguration getHostConfiguration(HttpClient client, MessageContext context, URL targetURL) { TransportClientProperties tcp = TransportClientPropertiesFactory.create(targetURL.getProtocol()); // http or https int port = targetURL.getPort(); boolean hostInNonProxyList = isHostInNonProxyList(targetURL.getHost(), tcp.getNonProxyHosts()); HostConfiguration config = new HostConfiguration(); if (port == -1) { if(targetURL.getProtocol().equalsIgnoreCase("https")) { port = 443; // default port for https being 443 } else { // it must be http port = 80; // default port for http being 80 } } if(hostInNonProxyList){ config.setHost(targetURL.getHost(), port, targetURL.getProtocol()); } else { if (tcp.getProxyHost().length() == 0 || tcp.getProxyPort().length() == 0) { config.setHost(targetURL.getHost(), port, targetURL.getProtocol()); } else { if (tcp.getProxyUser().length() != 0) { Credentials proxyCred = new UsernamePasswordCredentials(tcp.getProxyUser(), tcp.getProxyPassword()); // if the username is in the form "user\domain" // then use NTCredentials instead. int domainIndex = tcp.getProxyUser().indexOf("\\"); if (domainIndex > 0) { String domain = tcp.getProxyUser().substring(0, domainIndex); if (tcp.getProxyUser().length() > domainIndex + 1) { String user = tcp.getProxyUser().substring(domainIndex + 1); proxyCred = new NTCredentials(user, tcp.getProxyPassword(), tcp.getProxyHost(), domain); } } client.getState().setProxyCredentials(AuthScope.ANY, proxyCred); } int proxyPort = new Integer(tcp.getProxyPort()).intValue(); config.setProxy(tcp.getProxyHost(), proxyPort); } } return config; } /** * Extracts info from message context. * * @param method Post method * @param httpClient The client used for posting * @param msgContext the message context * @param tmpURL the url to post to. * * @throws Exception */ private void addContextInfo(HttpMethodBase method, HttpClient httpClient, MessageContext msgContext, URL tmpURL) throws Exception { // optionally set a timeout for the request if (msgContext.getTimeout() != 0) { /* ISSUE: these are not the same, but MessageContext has only one definition of timeout */ // SO_TIMEOUT -- timeout for blocking reads httpClient.getHttpConnectionManager().getParams().setSoTimeout(msgContext.getTimeout()); // timeout for initial connection httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(msgContext.getTimeout()); } // Get SOAPAction, default to "" String action = msgContext.useSOAPAction() ? msgContext.getSOAPActionURI() : ""; if (action == null) { action = ""; } Message msg = msgContext.getRequestMessage(); if (msg != null){ method.setRequestHeader(new Header(HTTPConstants.HEADER_CONTENT_TYPE, msg.getContentType(msgContext.getSOAPConstants()))); } method.setRequestHeader(new Header(HTTPConstants.HEADER_SOAP_ACTION, "\"" + action + "\"")); method.setRequestHeader(new Header(HTTPConstants.HEADER_USER_AGENT, Messages.getMessage("axisUserAgent"))); String userID = msgContext.getUsername(); String passwd = msgContext.getPassword(); // if UserID is not part of the context, but is in the URL, use // the one in the URL. if ((userID == null) && (tmpURL.getUserInfo() != null)) { String info = tmpURL.getUserInfo(); int sep = info.indexOf(':'); if ((sep >= 0) && (sep + 1 < info.length())) { userID = info.substring(0, sep); passwd = info.substring(sep + 1); } else { userID = info; } } if (userID != null) { Credentials proxyCred = new UsernamePasswordCredentials(userID, passwd); // if the username is in the form "user\domain" // then use NTCredentials instead. int domainIndex = userID.indexOf("\\"); if (domainIndex > 0) { String domain = userID.substring(0, domainIndex); if (userID.length() > domainIndex + 1) { String user = userID.substring(domainIndex + 1); proxyCred = new NTCredentials(user, passwd, NetworkUtils.getLocalHostname(), domain); } } httpClient.getState().setCredentials(AuthScope.ANY, proxyCred); } // add compression headers if needed if (msgContext.isPropertyTrue(HTTPConstants.MC_ACCEPT_GZIP)) { method.addRequestHeader(HTTPConstants.HEADER_ACCEPT_ENCODING, HTTPConstants.COMPRESSION_GZIP); } if (msgContext.isPropertyTrue(HTTPConstants.MC_GZIP_REQUEST)) { method.addRequestHeader(HTTPConstants.HEADER_CONTENT_ENCODING, HTTPConstants.COMPRESSION_GZIP); } // Transfer MIME headers of SOAPMessage to HTTP headers. MimeHeaders mimeHeaders = msg.getMimeHeaders(); if (mimeHeaders != null) { for (Iterator i = mimeHeaders.getAllHeaders(); i.hasNext(); ) { MimeHeader mimeHeader = (MimeHeader) i.next(); //HEADER_CONTENT_TYPE and HEADER_SOAP_ACTION are already set. //Let's not duplicate them. String headerName = mimeHeader.getName(); if (headerName.equals(HTTPConstants.HEADER_CONTENT_TYPE) || headerName.equals(HTTPConstants.HEADER_SOAP_ACTION)) { continue; } method.addRequestHeader(mimeHeader.getName(), mimeHeader.getValue()); } } // process user defined headers for information. Hashtable userHeaderTable = (Hashtable) msgContext.getProperty(HTTPConstants.REQUEST_HEADERS); if (userHeaderTable != null) { for (Iterator e = userHeaderTable.entrySet().iterator(); e.hasNext();) { Map.Entry me = (Map.Entry) e.next(); Object keyObj = me.getKey(); if (null == keyObj) { continue; } String key = keyObj.toString().trim(); String value = me.getValue().toString().trim(); if (key.equalsIgnoreCase(HTTPConstants.HEADER_EXPECT) && value.equalsIgnoreCase(HTTPConstants.HEADER_EXPECT_100_Continue)) { method.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, true); } else if (key.equalsIgnoreCase(HTTPConstants.HEADER_TRANSFER_ENCODING_CHUNKED)) { String val = me.getValue().toString(); if (null != val) { httpChunkStream = JavaUtils.isTrue(val); } } else { method.addRequestHeader(key, value); } } } } /** * Check if the specified host is in the list of non proxy hosts. *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -