Author: tyrell Date: Fri Feb 13 02:37:55 2009 New Revision: 30772 URL: http://wso2.org/svn/browse/wso2?view=rev&revision=30772
Log: Fixing MASHUP-849. Fixing the broken TwitterMap sample as a result. Modified: branches/mashup/java/1.5/java/modules/core/src/org/wso2/mashup/utils/MashupUtils.java branches/mashup/java/1.5/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/feed/FeedReader.java branches/mashup/java/1.5/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/system/SystemHostObject.java branches/mashup/java/1.5/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/wsrequest/WSRequestHostImpl.java branches/mashup/java/1.5/java/modules/samples/TwitterMap/TwitterMap.js Modified: branches/mashup/java/1.5/java/modules/core/src/org/wso2/mashup/utils/MashupUtils.java URL: http://wso2.org/svn/browse/wso2/branches/mashup/java/1.5/java/modules/core/src/org/wso2/mashup/utils/MashupUtils.java?rev=30772&r1=30771&r2=30772&view=diff ============================================================================== --- branches/mashup/java/1.5/java/modules/core/src/org/wso2/mashup/utils/MashupUtils.java (original) +++ branches/mashup/java/1.5/java/modules/core/src/org/wso2/mashup/utils/MashupUtils.java Fri Feb 13 02:37:55 2009 @@ -46,6 +46,9 @@ import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HostConfiguration; import org.apache.commons.httpclient.HttpMethod; +import org.apache.commons.httpclient.Credentials; +import org.apache.commons.httpclient.UsernamePasswordCredentials; +import org.apache.commons.httpclient.auth.AuthScope; import org.apache.commons.httpclient.protocol.ProtocolSocketFactory; import org.apache.commons.httpclient.protocol.Protocol; import org.apache.commons.httpclient.cookie.CookiePolicy; @@ -1339,7 +1342,7 @@ * @return The statusCode as an int * @throws IOException Thrown in case any exception occurrs */ - public static int executeHTTPMethod(HttpMethod method, URL targetURL) throws IOException { + public static int executeHTTPMethod(HttpMethod method, URL targetURL, String username, String password) throws IOException { MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); @@ -1389,6 +1392,14 @@ proxyConfiguration.configure(axisConfiguration, httpClient, config); } } + + // If a username and a password is provided we support basic auth + if ((username != null) && (password != null)) { + Credentials creds = new UsernamePasswordCredentials(username, password); + httpClient.getState() + .setCredentials(new AuthScope(targetURL.getHost(), port), creds); + } + return httpClient.executeMethod(config, method); } Modified: branches/mashup/java/1.5/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/feed/FeedReader.java URL: http://wso2.org/svn/browse/wso2/branches/mashup/java/1.5/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/feed/FeedReader.java?rev=30772&r1=30771&r2=30772&view=diff ============================================================================== --- branches/mashup/java/1.5/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/feed/FeedReader.java (original) +++ branches/mashup/java/1.5/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/feed/FeedReader.java Fri Feb 13 02:37:55 2009 @@ -74,42 +74,72 @@ FeedReader feedReaderObject = (FeedReader) thisObj; GetMethod method = new GetMethod(); if (arguments[0] instanceof String) { - String feedUrl = (String) arguments[0]; - try { - URL targetURL = new URL(feedUrl); - int statusCode = MashupUtils.executeHTTPMethod(method, targetURL); - if (statusCode != HttpStatus.SC_OK) { - throw new MashupFault( - "An error occured while getting the feed at " + targetURL + - ". Reason :" + - method.getStatusLine()); - } - InputStream response = method.getResponseBodyAsStream(); - SyndFeedInput input = new SyndFeedInput(); - SyndFeed feed = input.build(new XmlReader(response)); - retFeed = (Feed) cx.newObject(feedReaderObject, "Feed", new Object[0]); - retFeed.setFeed(feed); - } catch (SSLHandshakeException e) { - if (e.getMessage().indexOf("sun.security.validator.ValidatorException: PKIX path building failed") > -1) { - throw new MashupFault("An error occured while getting the feed since a " + - "trusted certificate was not found for the destination site \"" + - feedUrl + "\". You can manage your trusted certificates through " + - "the management UI", e); - } - throw new MashupFault("An error occured while getting the feed.", e); - } catch (FeedException e) { - throw new MashupFault("An error occured while getting the feed.", e); - } catch (HttpException e) { - throw new MashupFault("Fatal protocol violation: " + e.getMessage(), e); - } catch (IOException e) { - throw new MashupFault("Fatal transport error: " + e.getMessage(), e); - } finally { - // Release the connection. - method.releaseConnection(); - } + } else { throw new MashupFault("The parameter must be a string representing the Feeds URL"); } + String feedUrl = null; + String username = null; + String password = null; + + if ((arguments.length > 1) && (arguments.length < 4)) { + feedUrl = (String) arguments[0]; + + // We have a username password combo as well + if ((arguments[1] == null) | (!(arguments[1] instanceof String))) { + throw new MashupFault( + "The second argument for get function should be a string containing the username "); + } else { + username = (String) arguments[1]; + } + if ((arguments[2] == null) | (!(arguments[2] instanceof String))) { + throw new MashupFault( + "The third argument for get function should be a string containing the password "); + } else { + password = (String) arguments[2]; + } + } else if (arguments.length == 1) { + // We have only a URL + feedUrl = (String) arguments[0]; + } else { + throw new MashupFault( + "The get function should be called with either a single parameter which is the url to fetch the Feed from or three parameters, which are the url to fetch Feed from, the User Name and a Password for basic authentication."); + } + + try { + URL targetURL = new URL(feedUrl); + int statusCode = MashupUtils.executeHTTPMethod(method, targetURL, username, password); + if (statusCode != HttpStatus.SC_OK) { + throw new MashupFault( + "An error occured while getting the feed at " + targetURL + + ". Reason :" + + method.getStatusLine()); + } + InputStream response = method.getResponseBodyAsStream(); + SyndFeedInput input = new SyndFeedInput(); + SyndFeed feed = input.build(new XmlReader(response)); + retFeed = (Feed) cx.newObject(feedReaderObject, "Feed", new Object[0]); + retFeed.setFeed(feed); + } catch (SSLHandshakeException e) { + if (e.getMessage().indexOf( + "sun.security.validator.ValidatorException: PKIX path building failed") > -1) { + throw new MashupFault("An error occured while getting the feed since a " + + "trusted certificate was not found for the destination site \"" + + feedUrl + "\". You can manage your trusted certificates through " + + "the management UI", e); + } + throw new MashupFault("An error occured while getting the feed.", e); + } catch (FeedException e) { + throw new MashupFault("An error occured while getting the feed.", e); + } catch (HttpException e) { + throw new MashupFault("Fatal protocol violation: " + e.getMessage(), e); + } catch (IOException e) { + throw new MashupFault("Fatal transport error: " + e.getMessage(), e); + } finally { + // Release the connection. + method.releaseConnection(); + } + return retFeed; } } Modified: branches/mashup/java/1.5/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/system/SystemHostObject.java URL: http://wso2.org/svn/browse/wso2/branches/mashup/java/1.5/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/system/SystemHostObject.java?rev=30772&r1=30771&r2=30772&view=diff ============================================================================== --- branches/mashup/java/1.5/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/system/SystemHostObject.java (original) +++ branches/mashup/java/1.5/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/system/SystemHostObject.java Fri Feb 13 02:37:55 2009 @@ -259,7 +259,7 @@ HttpMethod method = new GetMethod(); try { URL url = uri.toURL(); - int statusCode = MashupUtils.executeHTTPMethod(method, url); + int statusCode = MashupUtils.executeHTTPMethod(method, url, null, null); if (statusCode != HttpStatus.SC_OK) { throw new MashupFault( "An error occured while getting the resource at " + url + ". Reason :" + method.getStatusLine()); } @@ -738,21 +738,49 @@ */ public static Object jsFunction_getXML(Context context, Scriptable thisObj, Object[] arguments, Function funObj) throws MashupFault { - if (arguments.length != 1 || arguments[0] == null || !(arguments[0] instanceof String)) { + if (arguments[0] == null || !(arguments[0] instanceof String)) { throw new MashupFault( - "The getXML function should be called with a single parameter which is the url to fetch XML from"); + "The getXML function should be called with either a single parameter which is the url to fetch XML from or three parameters, which are the url to fetch XML from, the User Name and a Password for basic authentication."); + } + + String urlString = null; + String username = null; + String password = null; + + if ((arguments.length > 1) && (arguments.length < 4)) { + urlString = (String) arguments[0]; + + // We have a username password combo as well + if ((arguments[1] == null) | (!(arguments[1] instanceof String))) { + throw new MashupFault( + "The second argument for getXML function should be a string containing the username "); + } else { + username = (String) arguments[1]; + } + if ((arguments[2] == null) | (!(arguments[2] instanceof String))) { + throw new MashupFault( + "The third argument for getXML function should be a string containing the password "); + } else { + password = (String) arguments[2]; + } + } else if (arguments.length == 1) { + // We have only a URL + urlString = (String) arguments[0]; + } else { + throw new MashupFault( + "The getXML function should be called with either a single parameter which is the url to fetch XML from or three parameters, which are the url to fetch XML from, the User Name and a Password for basic authentication."); } - String urlString = (String) arguments[0]; HttpMethod method = new GetMethod(); try { URL url = new URL(urlString); - int statusCode = MashupUtils.executeHTTPMethod(method, url); + int statusCode = MashupUtils.executeHTTPMethod(method, url, username, password); if (statusCode != HttpStatus.SC_OK) { - throw new MashupFault( - "An error occured while getting the resource at " + url + - ". Reason :" + method.getStatusLine()); + throw new MashupFault( + "An error occured while getting the resource at " + url + + ". Reason :" + method.getStatusLine()); } - StAXOMBuilder staxOMBuilder = new StAXOMBuilder(new ByteArrayInputStream(method.getResponseBody())); + StAXOMBuilder staxOMBuilder = + new StAXOMBuilder(new ByteArrayInputStream(method.getResponseBody())); OMElement omElement = staxOMBuilder.getDocumentElement(); Object[] objects = { omElement }; return context.newObject(thisObj, "XML", objects); @@ -952,7 +980,7 @@ throw new MashupFault( "The second argument should contain a String indicating the log level"); } - }else{ + } else { log.info(logMessage); } } Modified: branches/mashup/java/1.5/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/wsrequest/WSRequestHostImpl.java URL: http://wso2.org/svn/browse/wso2/branches/mashup/java/1.5/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/wsrequest/WSRequestHostImpl.java?rev=30772&r1=30771&r2=30772&view=diff ============================================================================== --- branches/mashup/java/1.5/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/wsrequest/WSRequestHostImpl.java (original) +++ branches/mashup/java/1.5/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/wsrequest/WSRequestHostImpl.java Fri Feb 13 02:37:55 2009 @@ -223,7 +223,7 @@ if (arguments[4] instanceof String) passwd = (String) arguments[4]; if (arguments[3] instanceof String) - username = (String) arguments[3]; + username = (String) arguments[3]; if (arguments[2] instanceof Boolean) wsRequest.async = ((Boolean) arguments[2]).booleanValue(); else @@ -238,7 +238,7 @@ } else if (arguments[2] instanceof Boolean) { wsRequest.async = ((Boolean) arguments[2]).booleanValue(); if (arguments[3] instanceof String) - username = (String) arguments[3]; + username = (String) arguments[3]; } else throw Context.reportRuntimeError("INVALID_SYNTAX_EXCEPTION"); break; @@ -349,11 +349,12 @@ if (mepObject != null && !(mepObject instanceof Undefined) && !(mepObject instanceof UniqueTag)) { - String mepValue = mepObject.toString(); + String mepValue = mepObject.toString(); if (IN_OUT.equalsIgnoreCase(mepValue) || IN_ONLY.equalsIgnoreCase(mepValue)) { wsRequest.mep = mepValue; } else { - throw new Error("Invalid value for mep. Supported values are in-out and in-only"); + throw new Error( + "Invalid value for mep. Supported values are in-out and in-only"); } } } @@ -375,12 +376,11 @@ } /** - * * This function enables you to give a WSDL and get WSRequest configured. You dont have to * configure it your self using an options object. */ public static void jsFunction_openWSDL(Context cx, Scriptable thisObj, Object[] arguments, - Function funObj) throws MashupFault { + Function funObj) throws MashupFault { WSRequestHostImpl wsRequest = checkInstance(thisObj); if (wsRequest.readyState > 0 && wsRequest.readyState < 4) { throw new Error("INVALID_STATE_EXCEPTION"); @@ -420,8 +420,7 @@ throw Context.reportRuntimeError("INVALID_SYNTAX_EXCEPTION"); if (arguments[2] instanceof NativeArray) { optionsArray = (NativeArray) arguments[2]; - } - else + } else throw Context.reportRuntimeError("INVALID_SYNTAX_EXCEPTION"); break; case 5: @@ -435,8 +434,7 @@ throw Context.reportRuntimeError("INVALID_SYNTAX_EXCEPTION"); if (arguments[2] instanceof NativeArray) { optionsArray = (NativeArray) arguments[2]; - } - else + } else throw Context.reportRuntimeError("INVALID_SYNTAX_EXCEPTION"); if (arguments[3] instanceof org.wso2.javascript.xmlimpl.QName) { org.wso2.javascript.xmlimpl.QName qName = @@ -460,16 +458,16 @@ HttpMethod method = new GetMethod(); try { URL url = new URL(wsdlURL); - int statusCode = MashupUtils.executeHTTPMethod(method, url); + int statusCode = MashupUtils.executeHTTPMethod(method, url, null, null); if (statusCode != HttpStatus.SC_OK) { - throw new MashupFault( - "An error occured while getting the resource at " + url + - ". Reason :" + method.getStatusLine()); - } - Document doc = XMLUtils.newDocument(method.getResponseBodyAsStream()); - WSDLReader reader = WSDLFactory.newInstance().newWSDLReader(); - reader.setFeature("javax.wsdl.importDocuments", true); - Definition definition = reader.readWSDL(getBaseURI(wsdlURL), doc); + throw new MashupFault( + "An error occured while getting the resource at " + url + + ". Reason :" + method.getStatusLine()); + } + Document doc = XMLUtils.newDocument(method.getResponseBodyAsStream()); + WSDLReader reader = WSDLFactory.newInstance().newWSDLReader(); + reader.setFeature("javax.wsdl.importDocuments", true); + Definition definition = reader.readWSDL(getBaseURI(wsdlURL), doc); Service service; Port returnPort; if (serviceQName == null) { @@ -483,13 +481,15 @@ } } if (service == null) { - throw Context.reportRuntimeError("The WSDL given does not contain any services " + - "that has ports"); + throw Context + .reportRuntimeError("The WSDL given does not contain any services " + + "that has ports"); } Map ports = service.getPorts(); Port port; returnPort = null; - for (Iterator portsIterator = ports.values().iterator(); (portsIterator.hasNext() && returnPort == null);) { + for (Iterator portsIterator = ports.values().iterator(); + (portsIterator.hasNext() && returnPort == null);) { port = (Port) portsIterator.next(); List extensibilityElements = port.getExtensibilityElements(); for (int i = 0; i < extensibilityElements.size(); i++) { @@ -516,7 +516,8 @@ if (extElement instanceof SOAP12Address) { // SOAP 1.2 address found - keep this and loop until http address is found returnPort = port; - String location = ((SOAP12Address) extElement).getLocationURI().trim(); + String location = + ((SOAP12Address) extElement).getLocationURI().trim(); if ((location != null) && location.startsWith("http:")) { // i.e we have found an http port so return from here break; @@ -605,14 +606,14 @@ } } wsRequest.targetNamespace = definition.getTargetNamespace(); - } catch (MalformedURLException e) { - throw new MashupFault(e); - } catch (Exception e) { - throw new MashupFault(e); - } finally { - // Release the connection. - method.releaseConnection(); - } + } catch (MalformedURLException e) { + throw new MashupFault(e); + } catch (Exception e) { + throw new MashupFault(e); + } finally { + // Release the connection. + method.releaseConnection(); + } setCommonProperties(cx, wsRequest); wsRequest.wsdlMode = true; } @@ -630,25 +631,25 @@ Options options = wsRequest.sender.getOptions(); if (options == null) { - options = new Options(); + options = new Options(); } options.setTimeOutInMilliSeconds(TIMEOUT_IN_MILLI_SECONDS); } private static String getBaseURI(String currentURI) { - try { - File file = new File(currentURI); - if (file.exists()) { - return file.getCanonicalFile().getParentFile().toURI() - .toString(); - } - String uriFragment = currentURI.substring(0, currentURI - .lastIndexOf("/")); - return uriFragment + (uriFragment.endsWith("/") ? "" : "/"); - } catch (IOException e) { - return null; - } - } + try { + File file = new File(currentURI); + if (file.exists()) { + return file.getCanonicalFile().getParentFile().toURI() + .toString(); + } + String uriFragment = currentURI.substring(0, currentURI + .lastIndexOf("/")); + return uriFragment + (uriFragment.endsWith("/") ? "" : "/"); + } catch (IOException e) { + return null; + } + } /** * <p/> @@ -670,7 +671,7 @@ WSRequestHostImpl wsRequest = (WSRequestHostImpl) thisObj; Object payload; QName operationName = null; - if (wsRequest.wsdlMode && arguments.length !=2) { + if (wsRequest.wsdlMode && arguments.length != 2) { throw new MashupFault("When the openWSDL method of WSRequest is used the send " + "function should be called with 2 parameters. The operation to invoke and " + "the payload"); @@ -679,7 +680,8 @@ payload = arguments[0]; } else if (arguments.length == 2) { if (arguments[0] instanceof org.wso2.javascript.xmlimpl.QName) { - org.wso2.javascript.xmlimpl.QName qName = (org.wso2.javascript.xmlimpl.QName) arguments[0]; + org.wso2.javascript.xmlimpl.QName qName = + (org.wso2.javascript.xmlimpl.QName) arguments[0]; String uri = (String) qName.get("uri", qName); String localName = (String) qName.get("localName", qName); operationName = new QName(uri, localName); @@ -689,7 +691,7 @@ } String localName = (String) arguments[0]; operationName = new QName(wsRequest.targetNamespace, localName); - }else { + } else { throw new Error("INVALID_STATE_EXCEPTION"); } payload = arguments[1]; @@ -735,7 +737,8 @@ try { if (wsRequest.async) { // asynchronous call to send() - AxisCallback callback = new WSRequestCallBack(Context.getCurrentContext(), wsRequest); + AxisCallback callback = + new WSRequestCallBack(Context.getCurrentContext(), wsRequest); if (wsRequest.wsdlMode) { engageSecurity(wsRequest, operationName); setSSLProperties(wsRequest); @@ -743,7 +746,8 @@ wsRequest.sender.fireAndForget(operationName, payloadElement); wsRequest.readyState = 4; } else { - wsRequest.sender.sendReceiveNonBlocking(operationName, payloadElement, callback); + wsRequest.sender + .sendReceiveNonBlocking(operationName, payloadElement, callback); wsRequest.readyState = 2; } } else { @@ -765,7 +769,8 @@ if (IN_ONLY.equalsIgnoreCase(wsRequest.mep)) { wsRequest.sender.fireAndForget(operationName, payloadElement); } else { - wsRequest.responseXML = wsRequest.sender.sendReceive(operationName, payloadElement); + wsRequest.responseXML = + wsRequest.sender.sendReceive(operationName, payloadElement); } wsRequest.readyState = 4; } else { @@ -996,7 +1001,8 @@ String httpInputSerialization, String httpContentEncodingString, String url, String username, - String passwd, String useSOAP, String useWSA, String useWSS, + String passwd, String useSOAP, String useWSA, + String useWSS, String action, NativeArray optionsArray, WSRequestHostImpl wsRequest) throws MashupFault { Options options = new Options(); @@ -1105,7 +1111,8 @@ } if (httpInputSerialization != null) { - options.setProperty(WSDL2Constants.ATTR_WHTTP_INPUT_SERIALIZATION, httpInputSerialization); + options.setProperty(WSDL2Constants.ATTR_WHTTP_INPUT_SERIALIZATION, + httpInputSerialization); options.setProperty(Constants.Configuration.MESSAGE_TYPE, httpInputSerialization); } @@ -1194,7 +1201,8 @@ } } - private RampartConfig getRampartConfig(WSRequestHostImpl wsRequest, String authorName) throws MashupFault { + private RampartConfig getRampartConfig(WSRequestHostImpl wsRequest, String authorName) + throws MashupFault { RampartConfig rc = new RampartConfig(); Modified: branches/mashup/java/1.5/java/modules/samples/TwitterMap/TwitterMap.js URL: http://wso2.org/svn/browse/wso2/branches/mashup/java/1.5/java/modules/samples/TwitterMap/TwitterMap.js?rev=30772&r1=30771&r2=30772&view=diff ============================================================================== --- branches/mashup/java/1.5/java/modules/samples/TwitterMap/TwitterMap.js (original) +++ branches/mashup/java/1.5/java/modules/samples/TwitterMap/TwitterMap.js Fri Feb 13 02:37:55 2009 @@ -23,51 +23,24 @@ fetchTwitterMap.documentation = "Fetches the list of recent Tweets. Requires Twitter authentication for 'Friends Only' mode." ; fetchTwitterMap.inputTypes = {"isFriendsOnly" : "boolean", "username" : "string", "password" : "string"}; -fetchTwitterMap.outputType = "xml"; -function fetchTwitterMap(isFriendsOnly, username, password){ - - var url = "http://twitter.com/statuses/public_timeline.xml"; - - var request = new WSRequest(); - - var options = new Array(); - options.useSOAP = false; - options.useWSA = false; - options.HTTPMethod = "GET"; - - if(isFriendsOnly){ - url = "http://twitter.com/statuses/friends_timeline.xml"; - options.username = username; - options.password = password; - } - - request.open(options, url, false); - request.send(null); - - return <twittermap>{request.responseXML}</twittermap>; +fetchTwitterMap.outputType = "xml"; +function fetchTwitterMap(isFriendsOnly, username, password){ + if(isFriendsOnly){ + return <twittermap>{system.getXML("http://twitter.com/statuses/friends_timeline.xml", username, password)}</twittermap>; + }else{ + return <twittermap>{system.getXML("http://twitter.com/statuses/public_timeline.xml")}</twittermap>; + } } - validateTwitterLogin.documentation = "Validates a username/password combination from Twitter." ; validateTwitterLogin.inputTypes = {"username" : "string", "password" : "string"}; -validateTwitterLogin.outputType = "xml"; +validateTwitterLogin.outputType = "xml"; function validateTwitterLogin(username, password){ - var request = new WSRequest(); - - var options = new Array(); - options.useSOAP = false; - options.useWSA = false; - options.HTTPMethod = "GET"; - options.username = username; - options.password = password; - - try{ - request.open(options, "http://twitter.com/account/verify_credentials.xml", false); - request.send(null); - return request.responseXML; - }catch(e){ - return <authorized>false</authorized>; - } - - + try{ + system.getXML("http://twitter.com/account/verify_credentials.xml", username, password); + return <authorized>true</authorized>; + }catch(e){ + return <authorized>false</authorized>; + } } + _______________________________________________ Mashup-dev mailing list [email protected] https://wso2.org/cgi-bin/mailman/listinfo/mashup-dev
