Author: keith Date: Mon Sep 1 23:27:05 2008 New Revision: 21318 URL: http://wso2.org/svn/browse/wso2?view=rev&revision=21318
Log: Fixing Mashup-1101. Introduced a ProxyConfiguration class that extends the one in axis2 in order to configure the proxy details. The one in axis2 relied on the message Context to configure it which is not available in the Mashup Server on some instances. Hence put in a new method so that we can configure it using the configuration context. Updating the places where ProxyCopnfiguration was used to use this newly added class. Added: trunk/mashup/java/modules/core/src/org/wso2/mashup/transport/http/MashupProxyConfiguration.java Modified: trunk/mashup/java/modules/core/src/org/wso2/mashup/utils/MashupUtils.java trunk/mashup/java/modules/coreservices/JSStubGeneratorService/src/org/wso2/mashup/JSStubGenerator.java trunk/mashup/java/modules/coreservices/scraperservice/src/org/wso2/mashup/coreservices/scraperservice/ScraperService.java trunk/mashup/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/system/SystemHostObject.java Added: trunk/mashup/java/modules/core/src/org/wso2/mashup/transport/http/MashupProxyConfiguration.java URL: http://wso2.org/svn/browse/wso2/trunk/mashup/java/modules/core/src/org/wso2/mashup/transport/http/MashupProxyConfiguration.java?pathrev=21318 ============================================================================== --- (empty file) +++ trunk/mashup/java/modules/core/src/org/wso2/mashup/transport/http/MashupProxyConfiguration.java Mon Sep 1 23:27:05 2008 @@ -0,0 +1,120 @@ +package org.wso2.mashup.transport.http; + +import org.apache.axis2.transport.http.ProxyConfiguration; +import org.apache.axis2.AxisFault; +import org.apache.axis2.engine.AxisConfiguration; +import org.apache.axis2.description.Parameter; +import org.apache.commons.httpclient.HttpClient; +import org.apache.commons.httpclient.HostConfiguration; +import org.apache.commons.httpclient.Credentials; +import org.apache.commons.httpclient.UsernamePasswordCredentials; +import org.apache.commons.httpclient.NTCredentials; +import org.apache.commons.httpclient.auth.AuthScope; +import org.apache.axiom.om.OMElement; + +import javax.xml.namespace.QName; + +public class MashupProxyConfiguration extends ProxyConfiguration { + + public void configure(AxisConfiguration axisConfiguration, HttpClient httpClient, + HostConfiguration config) throws AxisFault { + // <parameter name="Proxy"> + // <Configuration> + // <ProxyHost>example.org</ProxyHost> + // <ProxyPort>5678</ProxyPort> + // <ProxyUser>EXAMPLE\saminda</ProxyUser> + // <ProxyPassword>ppp</ProxyPassword> + // </Configuration> + // </parameter> + Credentials proxyCred = null; + + //Getting configuration values from Axis2.xml + Parameter param = axisConfiguration.getParameter(ATTR_PROXY); + + if (param != null) { + OMElement configurationEle = param.getParameterElement().getFirstElement(); + if (configurationEle == null) { + throw new AxisFault( + ProxyConfiguration.class.getName() + " Configuration element is missing"); + } + + OMElement proxyHostEle = + configurationEle.getFirstChildWithName(new QName(PROXY_HOST_ELEMENT)); + OMElement proxyPortEle = + configurationEle.getFirstChildWithName(new QName(PROXY_PORT_ELEMENT)); + OMElement proxyUserEle = + configurationEle.getFirstChildWithName(new QName(PROXY_USER_ELEMENT)); + OMElement proxyPasswordEle = + configurationEle.getFirstChildWithName(new QName(PROXY_PASSWORD_ELEMENT)); + + if (proxyHostEle == null) { + throw new AxisFault( + ProxyConfiguration.class.getName() + " ProxyHost element is missing"); + } + String text = proxyHostEle.getText(); + if (text == null) { + throw new AxisFault( + ProxyConfiguration.class.getName() + " ProxyHost's value is missing"); + } + + this.setProxyHost(text); + + if (proxyPortEle != null) { + this.setProxyPort(Integer.parseInt(proxyPortEle.getText())); + } + + if (proxyUserEle != null) { + this.setProxyUser(proxyUserEle.getText()); + } + + if (proxyPasswordEle != null) { + this.setProxyPassword(proxyPasswordEle.getText()); + } + + if (this.getProxyUser() == null && this.getProxyUser() == null) { + proxyCred = new UsernamePasswordCredentials("", ""); + } else { + proxyCred = + new UsernamePasswordCredentials(this.getProxyUser(), + this.getProxyPassword()); + } + + // if the username is in the form "DOMAIN\\user" + // then use NTCredentials instead. + if (this.getProxyUser() != null) { + int domainIndex = this.getProxyUser().indexOf("\\"); + if (domainIndex > 0) { + String domain = this.getProxyUser().substring(0, domainIndex); + if (this.getProxyUser().length() > domainIndex + 1) { + String user = this.getProxyUser().substring(domainIndex + 1); + proxyCred = new NTCredentials(user, + this.getProxyPassword(), + this.getProxyHost(), + domain); + } + } + } + } + + //Using Java Networking Properties + + String host = System.getProperty(HTTP_PROXY_HOST); + if (host != null) { + this.setProxyHost(host); + proxyCred = new UsernamePasswordCredentials("",""); + } + + String port = System.getProperty(HTTP_PROXY_PORT); + + if (port != null) { + this.setProxyPort(Integer.parseInt(port)); + } + + if (proxyCred == null) { + throw new AxisFault(ProxyConfiguration.class.getName() + + " Minimum proxy credentials are not set"); + } + httpClient.getState().setProxyCredentials(AuthScope.ANY, proxyCred); + config.setProxy(this.getProxyHost(), this.getProxyPort()); + } +} \ No newline at end of file Modified: trunk/mashup/java/modules/core/src/org/wso2/mashup/utils/MashupUtils.java URL: http://wso2.org/svn/browse/wso2/trunk/mashup/java/modules/core/src/org/wso2/mashup/utils/MashupUtils.java?rev=21318&r1=21317&r2=21318&view=diff ============================================================================== --- trunk/mashup/java/modules/core/src/org/wso2/mashup/utils/MashupUtils.java (original) +++ trunk/mashup/java/modules/core/src/org/wso2/mashup/utils/MashupUtils.java Mon Sep 1 23:27:05 2008 @@ -18,7 +18,6 @@ import org.owasp.validator.html.*; import org.apache.axiom.om.OMElement; -import org.apache.axiom.om.OMNode; import org.apache.axiom.om.impl.builder.StAXOMBuilder; import org.apache.axiom.om.util.UUIDGenerator; import org.apache.axis2.AxisFault; @@ -38,7 +37,6 @@ import org.apache.axis2.engine.AxisConfiguration; import org.apache.axis2.rpc.client.RPCServiceClient; import org.apache.axis2.transport.http.HTTPConstants; -import org.apache.axis2.transport.http.ProxyConfiguration; import org.apache.axis2.transport.http.util.URIEncoderDecoder; import org.apache.axis2.util.XMLChar; import org.apache.axis2.util.JavaUtils; @@ -60,6 +58,7 @@ import org.wso2.javascript.rhino.JavaScriptEngineConstants; import org.wso2.mashup.MashupConstants; import org.wso2.mashup.MashupFault; +import org.wso2.mashup.transport.http.MashupProxyConfiguration; import org.wso2.mashup.webapp.utils.RegistryUtils; import org.wso2.registry.RegistryConstants; import org.wso2.registry.exceptions.RegistryException; @@ -1383,12 +1382,13 @@ ConfigurationContext configurationContext = MashupUtils.getClientConfigurationContext(); if (configurationContext != null) { - Parameter parameter = configurationContext.getAxisConfiguration().getParameter(MashupConstants.PROXY); + AxisConfiguration axisConfiguration = configurationContext.getAxisConfiguration(); + Parameter parameter = axisConfiguration.getParameter(MashupConstants.PROXY); // The axis2.xml could have details of a proxy, If its present we should set those details // on the scraper too if (parameter != null) { - ProxyConfiguration proxyConfiguration = new ProxyConfiguration(); - proxyConfiguration.configure(MessageContext.getCurrentMessageContext(), httpClient, config); + MashupProxyConfiguration proxyConfiguration = new MashupProxyConfiguration(); + proxyConfiguration.configure(axisConfiguration, httpClient, config); } } return httpClient.executeMethod(config, method); Modified: trunk/mashup/java/modules/coreservices/JSStubGeneratorService/src/org/wso2/mashup/JSStubGenerator.java URL: http://wso2.org/svn/browse/wso2/trunk/mashup/java/modules/coreservices/JSStubGeneratorService/src/org/wso2/mashup/JSStubGenerator.java?rev=21318&r1=21317&r2=21318&view=diff ============================================================================== --- trunk/mashup/java/modules/coreservices/JSStubGeneratorService/src/org/wso2/mashup/JSStubGenerator.java (original) +++ trunk/mashup/java/modules/coreservices/JSStubGeneratorService/src/org/wso2/mashup/JSStubGenerator.java Mon Sep 1 23:27:05 2008 @@ -18,6 +18,7 @@ import org.apache.axiom.om.OMElement; import org.apache.axiom.om.OMNamespace; import org.apache.axis2.AxisFault; +import org.apache.axis2.engine.AxisConfiguration; import org.apache.axis2.context.ConfigurationContext; import org.apache.axis2.context.MessageContext; import org.apache.axis2.description.AxisEndpoint; @@ -28,7 +29,6 @@ import org.apache.axis2.description.WSDL11ToAxisServiceBuilder; import org.apache.axis2.description.WSDL2Constants; import org.apache.axis2.namespace.Constants; -import org.apache.axis2.transport.http.ProxyConfiguration; import org.apache.axis2.util.XMLUtils; import org.apache.axis2.util.XSLTTemplateProcessor; import org.apache.commons.httpclient.HttpClient; @@ -41,6 +41,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.mashup.utils.MashupUtils; +import org.wso2.mashup.transport.http.MashupProxyConfiguration; import javax.activation.DataHandler; import javax.xml.stream.XMLStreamException; @@ -107,14 +108,14 @@ ConfigurationContext configurationContext = MashupUtils.getClientConfigurationContext(); if (configurationContext != null) { - Parameter parameter = - configurationContext.getAxisConfiguration().getParameter("Proxy"); + AxisConfiguration axisConfiguration = configurationContext.getAxisConfiguration(); + Parameter parameter = axisConfiguration.getParameter("Proxy"); // The axis2.xml could have details of a proxy, If its present we should set those details // on the scraper too if (parameter != null) { - ProxyConfiguration proxyConfiguration = new ProxyConfiguration(); - proxyConfiguration.configure(MessageContext.getCurrentMessageContext(), - httpClient, httpClient.getHostConfiguration()); + MashupProxyConfiguration proxyConfiguration = new MashupProxyConfiguration(); + proxyConfiguration.configure(axisConfiguration, httpClient, + httpClient.getHostConfiguration()); } } int statusCode = httpClient.executeMethod(method); Modified: trunk/mashup/java/modules/coreservices/scraperservice/src/org/wso2/mashup/coreservices/scraperservice/ScraperService.java URL: http://wso2.org/svn/browse/wso2/trunk/mashup/java/modules/coreservices/scraperservice/src/org/wso2/mashup/coreservices/scraperservice/ScraperService.java?rev=21318&r1=21317&r2=21318&view=diff ============================================================================== --- trunk/mashup/java/modules/coreservices/scraperservice/src/org/wso2/mashup/coreservices/scraperservice/ScraperService.java (original) +++ trunk/mashup/java/modules/coreservices/scraperservice/src/org/wso2/mashup/coreservices/scraperservice/ScraperService.java Mon Sep 1 23:27:05 2008 @@ -21,7 +21,6 @@ import org.apache.axis2.context.MessageContext; import org.apache.axis2.description.Parameter; import org.apache.axis2.engine.AxisConfiguration; -import org.apache.axis2.transport.http.ProxyConfiguration; import org.apache.commons.httpclient.HttpClient; import org.webharvest.definition.ScraperConfiguration; import org.webharvest.exception.FileException; @@ -30,6 +29,7 @@ import org.webharvest.runtime.variables.Variable; import org.wso2.mashup.MashupFault; import org.wso2.mashup.MashupConstants; +import org.wso2.mashup.transport.http.MashupProxyConfiguration; import org.wso2.mashup.utils.MashupUtils; import org.xml.sax.InputSource; @@ -69,9 +69,10 @@ // The axis2.xml could have details of a proxy, If its present we should set those details // on the scraper too if (parameter != null) { - ProxyConfiguration proxyConfiguration = new ProxyConfiguration(); + MashupProxyConfiguration proxyConfiguration = new MashupProxyConfiguration(); HttpClient httpClient = scraper.getHttpClientManager().getHttpClient(); - proxyConfiguration.configure(context, httpClient, httpClient.getHostConfiguration()); + proxyConfiguration.configure(configuration, httpClient, + httpClient.getHostConfiguration()); } // Execute the scraper config Modified: trunk/mashup/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/system/SystemHostObject.java URL: http://wso2.org/svn/browse/wso2/trunk/mashup/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/system/SystemHostObject.java?rev=21318&r1=21317&r2=21318&view=diff ============================================================================== --- trunk/mashup/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/system/SystemHostObject.java (original) +++ trunk/mashup/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/system/SystemHostObject.java Mon Sep 1 23:27:05 2008 @@ -52,7 +52,6 @@ import java.io.BufferedReader; import java.io.File; import java.io.FileReader; -import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.io.ByteArrayInputStream; @@ -230,13 +229,12 @@ // Check whether this is a file in the service.resources directory if (f.exists() && !f.isDirectory()) { reader = new FileReader(f); + engine.setScriptName(path); + engine.evaluate(reader); } else { // This is not a file.. So we check whether this is a URL - reader = readFromURI(baseURI, path); + readFromURI(engine, baseURI, path); } - - engine.setScriptName(path); - engine.evaluate(reader); } catch (IOException e) { throw new MashupFault(e); } finally { @@ -245,7 +243,7 @@ } } - private static Reader readFromURI(URI baseURI, String path) throws MashupFault { + private static void readFromURI(JavaScriptEngine engine, URI baseURI, String path) throws MashupFault { Reader reader; // Not a file in the service.resources Dir.. Then check whether this is // a URI. @@ -258,16 +256,26 @@ // If this is a relative URL, then we resolves this relative to the // baseURI, if the URL is absolute we leave it as it is URI uri = baseURI.resolve(path); - InputStream inputStream; + HttpMethod method = new GetMethod(); try { - inputStream = uri.toURL().openStream(); - } catch (Exception e) { - // Catching all exceptions here, cause we need to present them - // nicely to user. - throw new MashupFault("Cannot read from given URI :" + uri.toString(), e); + URL url = uri.toURL(); + int statusCode = MashupUtils.executeHTTPMethod(method, url); + if (statusCode != HttpStatus.SC_OK) { + throw new MashupFault( + "An error occured while getting the resource at " + url + + ". Reason :" + method.getStatusLine()); + } + reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream())); + engine.setScriptName(path); + engine.evaluate(reader); + } catch (MalformedURLException e) { + throw new MashupFault(e); + } catch (IOException e) { + throw new MashupFault(e); + } finally { + // Release the connection. + method.releaseConnection(); } - reader = new BufferedReader(new InputStreamReader(inputStream)); - return reader; } private static SystemHostObject checkInstance(Scriptable obj) { _______________________________________________ Mashup-dev mailing list [email protected] http://mailman.wso2.org/cgi-bin/mailman/listinfo/mashup-dev
