You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by sa...@apache.org on 2005/10/04 07:08:56 UTC
svn commit: r293526 - in
/webservices/axis2/trunk/java/modules/core/src/org/apache/axis2/transport/http:
CommonsHTTPTransportSender.java HTTPConstants.java HTTPWorker.java
HttpTransportProperties.java
Author: saminda
Date: Mon Oct 3 22:08:42 2005
New Revision: 293526
URL: http://svn.apache.org/viewcvs?rev=293526&view=rev
Log:
1. HttpWorker being updated to hadle 202 Ok, for INOnly MEP.
2. Commons transport sender updated to handle Proxy and
basic NTLM server authentication using the capabilities of HttpClient.
Parameters/Properties are set from deployment time and runtime.
3. Proxy/NTLM authentication at runtime via an Object of
HttpTranportPropertis.
4. HttpConstants being updated to deal with the new constants available
to Proxy and NTLM authentication settings.
Added:
webservices/axis2/trunk/java/modules/core/src/org/apache/axis2/transport/http/HttpTransportProperties.java
Modified:
webservices/axis2/trunk/java/modules/core/src/org/apache/axis2/transport/http/CommonsHTTPTransportSender.java
webservices/axis2/trunk/java/modules/core/src/org/apache/axis2/transport/http/HTTPConstants.java
webservices/axis2/trunk/java/modules/core/src/org/apache/axis2/transport/http/HTTPWorker.java
Modified: webservices/axis2/trunk/java/modules/core/src/org/apache/axis2/transport/http/CommonsHTTPTransportSender.java
URL: http://svn.apache.org/viewcvs/webservices/axis2/trunk/java/modules/core/src/org/apache/axis2/transport/http/CommonsHTTPTransportSender.java?rev=293526&r1=293525&r2=293526&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/core/src/org/apache/axis2/transport/http/CommonsHTTPTransportSender.java (original)
+++ webservices/axis2/trunk/java/modules/core/src/org/apache/axis2/transport/http/CommonsHTTPTransportSender.java Mon Oct 3 22:08:42 2005
@@ -28,11 +28,13 @@
import org.apache.axis2.handlers.AbstractHandler;
import org.apache.axis2.i18n.Messages;
import org.apache.axis2.om.OMElement;
+import org.apache.axis2.om.OMAttribute;
import org.apache.axis2.om.impl.OMOutputImpl;
import org.apache.axis2.soap.SOAP11Constants;
import org.apache.axis2.soap.SOAP12Constants;
import org.apache.axis2.transport.TransportSender;
import org.apache.commons.httpclient.*;
+import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
@@ -43,12 +45,14 @@
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
+import javax.xml.namespace.QName;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
+import java.util.Iterator;
public class CommonsHTTPTransportSender
extends AbstractHandler
@@ -69,6 +73,16 @@
protected OMOutputImpl omOutput = new OMOutputImpl();
+ /**
+ * proxydiscription
+ *
+ */
+ protected TransportOutDescription proxyOutSetting = null;
+
+ protected static final String PROXY_HOST_NAME = "proxy_host";
+ protected static final String PROXY_PORT="proxy_port";
+
+
protected Log log = LogFactory.getLog(getClass().getName());
public CommonsHTTPTransportSender() {
@@ -206,17 +220,6 @@
}
- protected HostConfiguration getHostConfiguration(
- MessageContext context,
- URL targetURL) {
- //TODO cheaking wheather the host is a proxy
- HostConfiguration config = new HostConfiguration();
- config.setHost(
- targetURL.getHost(),
- targetURL.getPort() == -1 ? 80 : targetURL.getPort());
- return config;
- }
-
//get the contentLength...
public class AxisRequestEntity implements RequestEntity {
@@ -448,6 +451,8 @@
// timeout for initial connection
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(connectionTimeout);
+ //todo giving proxy and NTLM support
+
PostMethod postMethod = new PostMethod(url.toString());
postMethod.setPath(url.getFile());
@@ -500,7 +505,13 @@
}
}
- this.httpClient.executeMethod(postMethod);
+ /**
+ * main excecution takes place..
+ */
+
+ HostConfiguration config = this.getHostConfiguration(httpClient, msgContext, url);
+
+ this.httpClient.executeMethod(config,postMethod);
if (postMethod.getStatusCode() == HttpStatus.SC_OK) {
processResponse(postMethod, msgContext);
@@ -597,8 +608,10 @@
"text/xml; charset=" + charEncoding);
this.httpClient = new HttpClient();
- HostConfiguration hostConfig =
- this.getHostConfiguration(msgContext, url);
+
+
+ HostConfiguration hostConfig = this.getHostConfiguration(httpClient,msgContext, url);
+ //this.getHostConfiguration(msgContext, url);
//Get the timeout values set in the runtime
getTimoutValues(msgContext);
@@ -608,7 +621,10 @@
// timeout for initial connection
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(connectionTimeout);
- this.httpClient.executeMethod(hostConfig, getMethod);
+ /**
+ * with HostConfiguration
+ */
+ this.httpClient.executeMethod(hostConfig, getMethod,null);
if (getMethod.getStatusCode() == HttpStatus.SC_OK) {
processResponse(getMethod, msgContext);
@@ -673,4 +689,135 @@
}
-}
\ No newline at end of file
+ /**
+ * getting host configuration to support standard http/s, proxy and NTLM support
+ */
+ private HostConfiguration getHostConfiguration(HttpClient client, MessageContext msgCtx, URL targetURL) throws AxisFault {
+ boolean isHostProxy = isProxyListered(msgCtx); //list the proxy
+ int port = targetURL.getPort();
+ if ( port == -1 ) port = 80;
+ // to see the host is a proxy and in the proxy list - available in axis2.xml
+ HostConfiguration config = new HostConfiguration();
+
+ if (!isHostProxy) {
+ config.setHost(targetURL.getHost(),port,targetURL.getProtocol());
+ } else {
+ //proxy and NTLM configuration
+ this.configProxyAuthentication(client,proxyOutSetting,config,msgCtx);
+ }
+
+ return config;
+ }
+
+ private boolean isProxyListered(MessageContext msgCtx) throws AxisFault {
+ boolean returnValue =false;
+ Parameter par = null;
+ proxyOutSetting = msgCtx.getSystemContext()
+ .getAxisConfiguration()
+ .getTransportOut(new QName(Constants.TRANSPORT_HTTP));
+ if (proxyOutSetting != null) {
+ par = proxyOutSetting.getParameter(HTTPConstants.PROXY);
+ }
+ OMElement hostElement = null;
+ if (par != null) {
+ hostElement =
+ par.getParameterElement();
+ } else {
+ return returnValue;
+ }
+
+ if (hostElement != null) {
+ Iterator ite = hostElement.getAllAttributes();
+ while (ite.hasNext()) {
+ OMAttribute attribute = (OMAttribute) ite.next();
+ if (attribute.getLocalName().equalsIgnoreCase(PROXY_HOST_NAME)) {
+ returnValue = true;
+ }
+ }
+ }
+ HttpTransportProperties.ProxyProperties proxyProperties = null;
+ if ((proxyProperties = (HttpTransportProperties.ProxyProperties) msgCtx
+ .getProperty(HTTPConstants.PROXY)) != null) {
+ if (proxyProperties.getProxyHostName() != null) {
+ returnValue = true;
+ }
+ }
+ return returnValue;
+ }
+
+ /**
+ * Helper method to Proxy and NTLM authentication
+ * @param client
+ * @param proxySetting
+ * @param config
+ */
+
+ private void configProxyAuthentication(HttpClient client,
+ TransportOutDescription proxySetting,
+ HostConfiguration config,MessageContext msgCtx) throws AxisFault {
+ Parameter proxyParam = proxySetting.getParameter(HTTPConstants.PROXY);
+ String value = (String) proxyParam.getValue();
+ String split[] = value.split(":");
+ //values being hard coded due best practise
+ String usrName = split[0];
+ String domain = split[1];
+ String passwd = split[2];
+ //
+ Credentials proxyCred = null;
+
+ String proxyHostName = null;
+ int proxyPort = -1;
+
+ if (proxyParam != null) {
+ OMElement proxyParamElement = proxyParam.getParameterElement();
+ Iterator ite = proxyParamElement.getAllAttributes();
+ while (ite.hasNext()) {
+ OMAttribute att = (OMAttribute)ite.next();
+ if (att.getLocalName().equalsIgnoreCase(PROXY_HOST_NAME)){
+ proxyHostName = att.getValue();
+ }
+ if (att.getLocalName().equalsIgnoreCase(PROXY_PORT)) {
+ proxyPort = new Integer(att.getValue()).intValue();
+ }
+ }
+
+ }
+
+ if (domain.equals("") || domain == null || domain.equals("anonymous")) {
+ if (usrName.equals("anonymous") && passwd.equals("anonymous")) {
+ proxyCred = new UsernamePasswordCredentials("", "");
+ } else {
+ proxyCred = new UsernamePasswordCredentials(usrName,
+ passwd); //proxy
+ }
+ } else {
+ proxyCred = new NTCredentials(usrName, passwd, proxyHostName,
+ domain); //NTLM authentication with additionals prams
+ }
+
+
+
+ HttpTransportProperties.ProxyProperties proxyProperties = (HttpTransportProperties.ProxyProperties)msgCtx.getProperty(HTTPConstants.PROXY);
+ if ( proxyProperties != null) {
+ if (proxyProperties.getProxyPort() != -1) {
+ proxyPort = proxyProperties.getProxyPort();
+ }
+ if (proxyProperties.getProxyHostName().equals("") || proxyProperties.getProxyHostName() != null) {
+ proxyHostName = proxyProperties.getProxyHostName();
+ } else {
+ throw new AxisFault("Proxy Name is not valied");
+ }
+ if (proxyProperties.getUserName().equals("anonymous") || proxyProperties.getPassWord().equals("anonymous")){
+ proxyCred = new UsernamePasswordCredentials("","");
+ } else {
+ usrName = proxyProperties.getUserName();
+ passwd = proxyProperties.getPassWord();
+ domain = proxyProperties.getPassWord();
+ }
+ }
+ client.getState().setProxyCredentials(AuthScope.ANY, proxyCred);
+ config.setProxy(proxyHostName,proxyPort);
+ }
+
+}
+
Modified: webservices/axis2/trunk/java/modules/core/src/org/apache/axis2/transport/http/HTTPConstants.java
URL: http://svn.apache.org/viewcvs/webservices/axis2/trunk/java/modules/core/src/org/apache/axis2/transport/http/HTTPConstants.java?rev=293526&r1=293525&r2=293526&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/core/src/org/apache/axis2/transport/http/HTTPConstants.java (original)
+++ webservices/axis2/trunk/java/modules/core/src/org/apache/axis2/transport/http/HTTPConstants.java Mon Oct 3 22:08:42 2005
@@ -440,5 +440,7 @@
return data.getBytes();
}
+ public static final String PROXY = "PROXY";
+
}
Modified: webservices/axis2/trunk/java/modules/core/src/org/apache/axis2/transport/http/HTTPWorker.java
URL: http://svn.apache.org/viewcvs/webservices/axis2/trunk/java/modules/core/src/org/apache/axis2/transport/http/HTTPWorker.java?rev=293526&r1=293525&r2=293526&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/core/src/org/apache/axis2/transport/http/HTTPWorker.java (original)
+++ webservices/axis2/trunk/java/modules/core/src/org/apache/axis2/transport/http/HTTPWorker.java Mon Oct 3 22:08:42 2005
@@ -148,9 +148,19 @@
request.getRequestLine().getUri(),
configurationContext);
}
- response.setStatusLine(request.getRequestLine().getHttpVersion(), 200, "OK");
+
+ Object contextWritten = msgContext.getOperationContext().getProperty(Constants.RESPONSE_WRITTEN);
+ if (contextWritten != null &&
+ Constants.VALUE_TRUE.equals(contextWritten)) {
+ response.setStatusLine(
+ request.getRequestLine().getHttpVersion(), 200, "OK");
+ } else {
+ response.setStatusLine(
+ request.getRequestLine().getHttpVersion(), 202, "OK");
+ }
response.setBody(new ByteArrayInputStream(baos.toByteArray()));
- setResponseHeaders(conn, request, response,baos.toByteArray().length);
+ setResponseHeaders(conn, request, response,
+ baos.toByteArray().length);
conn.writeResponse(response);
} catch (Throwable e) {
try {
@@ -198,9 +208,12 @@
}
}
}
- if (!response.containsHeader("Transfer-Encoding")){
- Header header = new Header("Content-Length",String.valueOf(contentLength));
- response.addHeader(header);
+ if (!response.containsHeader("Transfer-Encoding")) {
+ if (contentLength != 0) {
+ Header header = new Header("Content-Length",
+ String.valueOf(contentLength));
+ response.addHeader(header);
+ }
}
}
Added: webservices/axis2/trunk/java/modules/core/src/org/apache/axis2/transport/http/HttpTransportProperties.java
URL: http://svn.apache.org/viewcvs/webservices/axis2/trunk/java/modules/core/src/org/apache/axis2/transport/http/HttpTransportProperties.java?rev=293526&view=auto
==============================================================================
--- webservices/axis2/trunk/java/modules/core/src/org/apache/axis2/transport/http/HttpTransportProperties.java (added)
+++ webservices/axis2/trunk/java/modules/core/src/org/apache/axis2/transport/http/HttpTransportProperties.java Mon Oct 3 22:08:42 2005
@@ -0,0 +1,111 @@
+package org.apache.axis2.transport.http;
+/*
+ * Copyright 2004,2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import org.apache.commons.httpclient.HttpVersion;
+
+/**
+ * Utility bean for setting transport properties in runtime.
+ */
+public class HttpTransportProperties {
+ protected boolean chunked;
+ protected HttpVersion httpVersion;
+ protected String protocol;
+
+ public HttpTransportProperties(){}
+
+ public void setChunked(boolean chunked){
+ this.chunked = chunked;
+ }
+ public void setHttpVersion(HttpVersion httpVerion){
+ this.httpVersion = httpVerion;
+ }
+ public void setProtocol(String protocol){
+ this.protocol = protocol;
+ }
+
+ public boolean getChunked(){
+ return chunked;
+ }
+ public HttpVersion getHttpVersion(){
+ return httpVersion;
+ }
+ public String getProtocol(){
+ return protocol;
+ }
+
+ public class ProxyProperties {
+ protected String proxyHostName;
+ protected int proxyPort = -1;
+
+ protected String userName;
+ protected String domain;
+ protected String passWord;
+
+ public ProxyProperties() {}
+
+ public void setUserName(String userName){
+ this.userName = userName;
+ }
+ public void setDomain(String domain){
+ this.domain = domain;
+ }
+ public void setPassWord(String passWord){
+ this.passWord = passWord;
+ }
+
+ public void setProxyName(String proxyHostName){
+ this.proxyHostName = proxyHostName;
+ }
+ public void setProxyPort(int proxyPort){
+ this.proxyPort = proxyPort;
+ }
+
+ public String getProxyHostName(){
+ return proxyHostName;
+ }
+ public int getProxyPort(){
+ return proxyPort;
+ }
+
+ public String getUserName() {
+ if (userName.equals("") || userName == null) {
+ return "anonymous";
+ } else {
+ return userName;
+ }
+ }
+
+ public String getDomain() {
+ if (domain.equals("") || domain == null) {
+ return "anonymous";
+ } else {
+ return domain;
+ }
+ }
+
+ public String getPassWord() {
+ if (passWord.equals("") || passWord == null) {
+ return "anonymous";
+ } else {
+ return passWord;
+ }
+ }
+
+ }
+
+
+}