So I use it like this:
<soap:invokeraw endpoint="http://localhost/some/service" var="response">
<soap:Envelope ....>
<soap:Header>
Your headers
</soap:Header>
<soap:Body>
<ns:yourMethod />
</soap:Body>
</soap:Envelope>
</soap:invokeraw>Also in the patch is username/password support for the invoke tag. That one isn't well tested though.
Cheers,
- Dan
Dan Diephouse wrote:
I've been looking at using Jelly to test my soap services, but am trying to come up with a slick way of integrating soap headers into the request. Anyone got any great ideas?
It would be really easy to use just the normal authentication (Call.setUsername/Call.setPassword).. just add username/password attributes to the <soap:invoke/> tag. However, I need to use headers to authenticate.
The only way I could think to get around this would be to construct the whole soap request myself. For example there could be an invoke tag which instead of just taking parameters to the method, took a whole soap request:
<soap:invoke target="http://localhost/some/service">
<soap:Envelope...> <soap:Header>..... etc etc.
</soap:invoke>
Is that feasible? Or is there a Better Way? :)
- Dan
Index: project.xml
===================================================================
RCS file: /home/cvspublic/jakarta-commons/jelly/jelly-tags/soap/project.xml,v
retrieving revision 1.5
diff -u -r1.5 project.xml
--- project.xml 27 Jan 2003 05:24:15 -0000 1.5
+++ project.xml 24 Jun 2003 15:35:32 -0000
@@ -25,6 +25,11 @@
<!-- START for compilation -->
<dependency>
+ <id>commons-httpclient</id>
+ <version>2.0-beta1</version>
+ </dependency>
+
+ <dependency>
<id>commons-jelly</id>
<version>SNAPSHOT</version>
</dependency>
Index: src/java/org/apache/commons/jelly/tags/soap/InvokeRawTag.java
===================================================================
RCS file: src/java/org/apache/commons/jelly/tags/soap/InvokeRawTag.java
diff -N src/java/org/apache/commons/jelly/tags/soap/InvokeRawTag.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ src/java/org/apache/commons/jelly/tags/soap/InvokeRawTag.java 24 Jun 2003
15:35:33 -0000
@@ -0,0 +1,216 @@
+/*
+ * $Header:
/home/cvspublic/jakarta-commons/jelly/jelly-tags/soap/src/java/org/apache/commons/jelly/tags/soap/InvokeTag.java,v
1.2 2003/01/26 07:08:45 morgand Exp $
+ * $Revision: 1.2 $
+ * $Date: 2003/01/26 07:08:45 $
+ *
+ * ====================================================================
+ *
+ * The Apache Software License, Version 1.1
+ *
+ * Copyright (c) 2002 The Apache Software Foundation. All rights
+ * reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * 3. The end-user documentation included with the redistribution, if
+ * any, must include the following acknowlegement:
+ * "This product includes software developed by the
+ * Apache Software Foundation (http://www.apache.org/)."
+ * Alternately, this acknowlegement may appear in the software itself,
+ * if and wherever such third-party acknowlegements normally appear.
+ *
+ * 4. The names "The Jakarta Project", "Commons", and "Apache Software
+ * Foundation" must not be used to endorse or promote products derived
+ * from this software without prior written permission. For written
+ * permission, please contact [EMAIL PROTECTED]
+ *
+ * 5. Products derived from this software may not be called "Apache"
+ * nor may "Apache" appear in their names without prior written
+ * permission of the Apache Group.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ * $Id: InvokeTag.java,v 1.2 2003/01/26 07:08:45 morgand Exp $
+ */
+package org.apache.commons.jelly.tags.soap;
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.rmi.RemoteException;
+
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpException;
+import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
+import org.apache.commons.httpclient.methods.PostMethod;
+import org.apache.commons.jelly.JellyTagException;
+import org.apache.commons.jelly.MissingAttributeException;
+import org.apache.commons.jelly.TagSupport;
+import org.apache.commons.jelly.XMLOutput;
+
+/**
+ * Invokes a web service
+ *
+ * @author <a href="mailto:[EMAIL PROTECTED]">James Birchfield</a>
+ * @version $Revision: 1.2 $
+ */
+public class InvokeRawTag extends TagSupport
+{
+
+ private String var;
+ private String endpoint = null;
+ private String soapAction;
+
+ /**
+ * @param action
+ */
+ public void setSoapAction(String action)
+ {
+ soapAction = action;
+ }
+
+ public InvokeRawTag()
+ {
+ }
+
+ // Tag interface
+ //-------------------------------------------------------------------------
+ public void doTag(XMLOutput output)
+ throws MissingAttributeException, JellyTagException
+ {
+ if (endpoint == null)
+ {
+ throw new MissingAttributeException("endpoint");
+ }
+
+ String request = getBodyText();
+
+ Object answer = null;
+ try
+ {
+ // Prepare HTTP post
+ PostMethod post = new PostMethod(endpoint);
+
+ // Request content will be retrieved directly
+ // from the input stream
+ post.setRequestBody(new StringInputStream(request));
+
+ // Per default, the request content needs to be buffered
+ // in order to determine its length.
+ // Request body buffering can be avoided when
+ // = content length is explicitly specified
+ // = chunk-encoding is used
+ if (request.length() < Integer.MAX_VALUE)
+ {
+ post.setRequestContentLength((int) request.length());
+ }
+ else
+ {
+ post.setRequestContentLength(
+ EntityEnclosingMethod.CONTENT_LENGTH_CHUNKED);
+ }
+
+ // Specify content type and encoding
+ // If content encoding is not explicitly specified
+ // ISO-8859-1 is assumed
+ post.setRequestHeader(
+ "Content-type",
+ "text/xml; charset=ISO-8859-1");
+
+ // Set the SOAPAction header
+ if ( soapAction == null )
+ {
+ post.setRequestHeader( "SOAPAction", "");
+ }
+ else
+ {
+ post.setRequestHeader( "SOAPAction", soapAction);
+ }
+
+ // Get HTTP client
+ HttpClient httpclient = new HttpClient();
+ // Execute request
+ int result = httpclient.executeMethod(post);
+
+ var = post.getResponseBodyAsString();
+
+ System.err.println("Response was: ");
+ System.err.println(var);
+
+ // Release current connection to the connection pool once you are done
+ post.releaseConnection();
+ }
+ catch (MalformedURLException e)
+ {
+ throw new JellyTagException(e);
+ }
+ catch (RemoteException e)
+ {
+ throw new JellyTagException(e);
+ }
+ catch (HttpException e)
+ {
+ throw new JellyTagException(e);
+ }
+ catch (IOException e)
+ {
+ throw new JellyTagException(e);
+ }
+
+ if (var != null)
+ {
+ context.setVariable(var, answer);
+ }
+ else
+ {
+ // should turn the answer into XML events...
+ throw new JellyTagException(
+ "Not implemented yet; should stream results as XML events. Results: "
+ + answer);
+ }
+ }
+
+ // Properties
+ //-------------------------------------------------------------------------
+ /**
+ * Sets the end point to which the invocation will occur
+ */
+ public void setEndpoint(String endpoint)
+ {
+ this.endpoint = endpoint;
+ }
+
+ /**
+ * Sets the name of the variable to output the results of the SOAP call to.
+ */
+ public void setVar(String var)
+ {
+ this.var = var;
+ }
+}
Index: src/java/org/apache/commons/jelly/tags/soap/InvokeTag.java
===================================================================
RCS file:
/home/cvspublic/jakarta-commons/jelly/jelly-tags/soap/src/java/org/apache/commons/jelly/tags/soap/InvokeTag.java,v
retrieving revision 1.2
diff -u -r1.2 InvokeTag.java
--- src/java/org/apache/commons/jelly/tags/soap/InvokeTag.java 26 Jan 2003 07:08:45
-0000 1.2
+++ src/java/org/apache/commons/jelly/tags/soap/InvokeTag.java 24 Jun 2003 15:35:33
-0000
@@ -87,9 +87,11 @@
private String endpoint = null;
private String namespace = null;
private String method = null;
+ private String username;
+ private String password;
private Service service;
private Object params;
-
+
public InvokeTag() {
}
@@ -129,6 +131,12 @@
call.setTargetEndpointAddress(new java.net.URL(endpoint));
call.setOperationName(new QName(namespace, method));
+ if ( username != null && !username.equals("") )
+ {
+ call.setUsername( username );
+ call.setPassword( password );
+ }
+
answer = call.invoke(params);
}
catch (MalformedURLException e) {
@@ -201,6 +209,23 @@
public void setParams(Object params) {
this.params = params;
}
+
+ /**
+ * Set the password for the SOAP call.
+ */
+ public void setPassword(String password)
+ {
+ this.password = password;
+ }
+
+ /**
+ * Set the username for the SOAP call.
+ */
+ public void setUsername(String username)
+ {
+ this.username = username;
+ }
+
// Implementation methods
//-------------------------------------------------------------------------
Index: src/java/org/apache/commons/jelly/tags/soap/SoapTagLibrary.java
===================================================================
RCS file:
/home/cvspublic/jakarta-commons/jelly/jelly-tags/soap/src/java/org/apache/commons/jelly/tags/soap/SoapTagLibrary.java,v
retrieving revision 1.1
diff -u -r1.1 SoapTagLibrary.java
--- src/java/org/apache/commons/jelly/tags/soap/SoapTagLibrary.java 7 Jan 2003
14:10:03 -0000 1.1
+++ src/java/org/apache/commons/jelly/tags/soap/SoapTagLibrary.java 24 Jun 2003
15:35:33 -0000
@@ -72,5 +72,6 @@
{
public SoapTagLibrary() {
registerTag("invoke", InvokeTag.class);
+ registerTag("invokeraw", InvokeRawTag.class);
}
}
Index: src/java/org/apache/commons/jelly/tags/soap/StringInputStream.java
===================================================================
RCS file: src/java/org/apache/commons/jelly/tags/soap/StringInputStream.java
diff -N src/java/org/apache/commons/jelly/tags/soap/StringInputStream.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ src/java/org/apache/commons/jelly/tags/soap/StringInputStream.java 24 Jun 2003
15:35:33 -0000
@@ -0,0 +1,146 @@
+package org.apache.commons.jelly.tags.soap;
+
+/* ====================================================================
+ * The Apache Software License, Version 1.1
+ *
+ * Copyright (c) 2001 The Apache Software Foundation. All rights
+ * reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * 3. The end-user documentation included with the redistribution,
+ * if any, must include the following acknowledgment:
+ * "This product includes software developed by the
+ * Apache Software Foundation (http://www.apache.org/)."
+ * Alternately, this acknowledgment may appear in the software itself,
+ * if and wherever such third-party acknowledgments normally appear.
+ *
+ * 4. The names "Apache" and "Apache Software Foundation" and
+ * "Apache MavenSession" must not be used to endorse or promote products
+ * derived from this software without prior written permission. For
+ * written permission, please contact [EMAIL PROTECTED]
+ *
+ * 5. Products derived from this software may not be called "Apache",
+ * "Apache MavenSession", nor may "Apache" appear in their name, without
+ * prior written permission of the Apache Software Foundation.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ * ====================================================================
+ */
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.StringReader;
+
+/**
+ * Wraps a String as an InputStream. Note that data will be lost for
+ * characters not in ISO Latin 1, as a simple char->byte mapping is assumed.
+ *
+ * @author <a href="mailto:[EMAIL PROTECTED]">Magesh Umasankar</a>
+ */
+public class StringInputStream
+ extends InputStream
+{
+ /** Source string, stored as a StringReader */
+ private StringReader in;
+
+ /**
+ * Composes a stream from a String
+ *
+ * @param source The string to read from. Must not be <code>null</code>.
+ */
+ public StringInputStream( String source )
+ {
+ in = new StringReader( source );
+ }
+
+ /**
+ * Reads from the Stringreader, returning the same value. Note that
+ * data will be lost for characters not in ISO Latin 1. Clients
+ * assuming a return value in the range -1 to 255 may even fail on
+ * such input.
+ *
+ * @return the value of the next character in the StringReader
+ *
+ * @exception IOException if the original StringReader fails to be read
+ */
+ public int read() throws IOException
+ {
+ return in.read();
+ }
+
+ /**
+ * Closes the Stringreader.
+ *
+ * @exception IOException if the original StringReader fails to be closed
+ */
+ public void close() throws IOException
+ {
+ in.close();
+ }
+
+ /**
+ * Marks the read limit of the StringReader.
+ *
+ * @param limit the maximum limit of bytes that can be read before the
+ * mark position becomes invalid
+ */
+ public synchronized void mark( final int limit )
+ {
+ try
+ {
+ in.mark( limit );
+ }
+ catch ( IOException ioe )
+ {
+ throw new RuntimeException( ioe.getMessage() );
+ }
+ }
+
+ /**
+ * Resets the StringReader.
+ *
+ * @exception IOException if the StringReader fails to be reset
+ */
+ public synchronized void reset() throws IOException
+ {
+ in.reset();
+ }
+
+ /**
+ * @see InputStream#markSupported
+ */
+ public boolean markSupported()
+ {
+ return in.markSupported();
+ }
+}
+--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
