Author: richard.opalka(a)jboss.com
Date: 2008-04-25 05:07:01 -0400 (Fri, 25 Apr 2008)
New Revision: 6681
Modified:
stack/metro/trunk/src/main/java/org/jboss/wsf/stack/metro/MessageStreamContext.java
stack/metro/trunk/src/main/java/org/jboss/wsf/stack/metro/RequestHandlerImpl.java
Log:
refactoring + added javadoc, no functional change
Modified:
stack/metro/trunk/src/main/java/org/jboss/wsf/stack/metro/MessageStreamContext.java
===================================================================
---
stack/metro/trunk/src/main/java/org/jboss/wsf/stack/metro/MessageStreamContext.java 2008-04-25
09:04:23 UTC (rev 6680)
+++
stack/metro/trunk/src/main/java/org/jboss/wsf/stack/metro/MessageStreamContext.java 2008-04-25
09:07:01 UTC (rev 6681)
@@ -27,11 +27,10 @@
/**
* @author Heiko Braun
*/
-public class MessageStreamContext
+class MessageStreamContext
{
-
public static final int STATUS_INTERNAL_SERVER_ERROR = 500;
- public final static String CONTENT_TYPE = "Content-Type";
+ public static final String CONTENT_TYPE = "Content-Type";
private Map<String, String> requestContext = new HashMap<String,
String>();
private Map<String, String> responseContext = new HashMap<String,
String>();
Modified:
stack/metro/trunk/src/main/java/org/jboss/wsf/stack/metro/RequestHandlerImpl.java
===================================================================
---
stack/metro/trunk/src/main/java/org/jboss/wsf/stack/metro/RequestHandlerImpl.java 2008-04-25
09:04:23 UTC (rev 6680)
+++
stack/metro/trunk/src/main/java/org/jboss/wsf/stack/metro/RequestHandlerImpl.java 2008-04-25
09:07:01 UTC (rev 6681)
@@ -40,16 +40,17 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Writer;
+import java.util.Map;
import java.util.Properties;
import java.net.URL;
/**
- * A request handler
+ * Request handler that delegates to Metro's ServletAdapter.
*
* @author Thomas.Diesler(a)jboss.org
* @since 25-Apr-2007
*/
-public class RequestHandlerImpl implements RequestHandler
+class RequestHandlerImpl implements RequestHandler
{
// provide logging
private static final Logger log = Logger.getLogger(RequestHandlerImpl.class);
@@ -58,24 +59,33 @@
{
}
- public void handleHttpRequest(Endpoint endpoint, HttpServletRequest req,
HttpServletResponse res, ServletContext context) throws ServletException, IOException
+ /**
+ * Handles HTTP requests. It supports <b>POST</b> and
<b>GET</b> HTTP methods only.
+ * @param endpoint endpoint
+ * @param req servlet request to handle
+ * @param res servlet response to return
+ * @param servletCtx servlet context
+ * @throws ServletException when some problem occurs
+ * @throws IOException when some IO problem occurs
+ */
+ public void handleHttpRequest(Endpoint endpoint, HttpServletRequest req,
HttpServletResponse res, ServletContext servletCtx)
+ throws ServletException, IOException
{
ServletAdapter target = endpoint.getAttachment(ServletAdapter.class);
if(null == target)
- throw new IllegalArgumentException("Cannot obtain ServletAdapter");
+ throw new IllegalStateException("Cannot obtain ServletAdapter");
-
EndpointAssociation.setEndpoint(endpoint);
try
{
String method = req.getMethod();
if (method.equals("POST"))
{
- doPost(target, context, req, res);
+ doPost(target, servletCtx, req, res);
}
else if(method.equals("GET"))
{
- doGet(target, req, context, res);
+ doGet(target, req, servletCtx, res);
}
else
{
@@ -88,15 +98,99 @@
}
}
- private void doGet(ServletAdapter target, HttpServletRequest req, ServletContext
context, HttpServletResponse res)
- throws ServletException
+ /**
+ * The InvocationContext accepts a {@link Properties} attachment that can carry
request properties.<br/>
+ * The properties keys are derived from {@link
org.jboss.wsf.stack.metro.MessageStreamContext}
+ * @param endpoint endpoint
+ * @param inStream input stream
+ * @param outStream output stream
+ * @param invCtx invocation context
+ */
+ public void handleRequest(Endpoint endpoint, InputStream inStream, OutputStream
outStream, InvocationContext invCtx)
{
- try {
+ MessageStreamAdapter adapter = endpoint.getAttachment(MessageStreamAdapter.class);
+ if (adapter == null)
+ throw new IllegalStateException("Cannot obtain: " +
adapter.getClass().getName());
+ try
+ {
+ // Hacky, but the InvokerJSE requires it.
+ // It's better to do it here than outside the RequestHandler.
+ EndpointAssociation.setEndpoint(endpoint);
+
+ MessageStreamContext streamContext = new MessageStreamContext();
+ copyProperties(invCtx, streamContext);
+ adapter.handle(streamContext, inStream, outStream );
+ }
+ catch (IOException e)
+ {
+ throw new WebServiceException("Failed to process request: " +
e.getMessage(), e);
+ }
+ finally
+ {
+ EndpointAssociation.removeEndpoint();
+ }
+ }
+
+ /**
+ * Handles HTTP get request. It obtains endpoint's address and constructs URL with
<b>?wsdl</b> query string.
+ * The constructed URL is used to create the input stream to read WSDL content from
and submited to the user.
+ * @param endpoint endpoint
+ * @param outStream output stream
+ * @param invCtx invocation context
+ */
+ public void handleWSDLRequest(Endpoint endpoint, OutputStream outStream,
InvocationContext invCtx)
+ {
+ String endpointAddress = endpoint.getAddress();
+ if (endpointAddress == null)
+ throw new IllegalArgumentException("Invalid endpoint address: " +
endpointAddress);
+
+ InputStream inStream = null;
+ try
+ {
+ URL wsdlUrl = new URL(endpointAddress + "?wsdl");
+ inStream = wsdlUrl.openStream();
+ IOUtils.copyStream(outStream, inStream);
+ }
+ catch (IOException e)
+ {
+ throw new WebServiceException("Failed to process WSDL request: " +
e.getMessage(), e);
+ }
+ finally
+ {
+ // close input stream when available
+ try
+ {
+ if(inStream!=null) inStream.close();
+ }
+ catch (IOException ignore) {}
+ // close output stream when available
+ try
+ {
+ if(outStream!=null) outStream.close();
+ }
+ catch (IOException ignore) {}
+ }
+ }
+
+ /**
+ * Handles HTTP GET request using Metro's ServletAdapter
<b>publishWSDL</b> method
+ * @param target Metro's ServletAdapter
+ * @param req request message
+ * @param context servlet context
+ * @param res response message
+ * @throws ServletException if some problem occurs
+ */
+ private static void doGet(ServletAdapter target, HttpServletRequest req,
ServletContext context, HttpServletResponse res)
+ throws ServletException
+ {
+ try
+ {
if (target != null)
{
String query = req.getQueryString();
- if (isMetadataQuery(query)) {
+ if (isMetadataQuery(query))
+ {
// Sends published WSDL and schema documents
target.publishWSDL(context, req, res);
return;
@@ -118,18 +212,16 @@
}
}
- private static void sendResponse(int status, String message, HttpServletResponse res)
- throws IOException
+ /**
+ * Handles HTTP POST request using Metro's ServletAdapter
<b>handle</b> method
+ * @param target Metro's ServletAdapter
+ * @param req request message
+ * @param context servlet context
+ * @param res response message
+ * @throws ServletException if some problem occurs
+ */
+ private static void doPost(ServletAdapter target, ServletContext context,
HttpServletRequest req, HttpServletResponse res)
{
- res.setStatus(status);
- res.setContentType("text/plain");
- Writer out = res.getWriter();
- out.write(message);
- out.close();
- }
-
- private void doPost(ServletAdapter target, ServletContext context, HttpServletRequest
req, HttpServletResponse res)
- {
try
{
target.handle(context, req, res);
@@ -140,107 +232,57 @@
res.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
}
-
+
/**
- * The InvocationContext accepts a {@link Properties} attachment that can carry
request properies.<br/>
- * Property keys are derived from {@link
org.jboss.wsf.stack.metro.MessageStreamContext}
- * <p/>
- * Required propeties are:
- *
- * <ul>
- * <li><tt>Content-Type</tt> ({@link
org.jboss.wsf.stack.metro.MessageStreamContext#CONTENT_TYPE})
- * </ul>
- * @param endpoint
- * @param inStream
- * @param outStream
- * @param context
+ * Copies properties from invocation context to message context when properties are
available
+ * @param invCtx invocation context
+ * @param msgCtx message context
*/
- public void handleRequest(Endpoint endpoint, InputStream inStream, OutputStream
outStream, InvocationContext context)
+ private static void copyProperties(InvocationContext invCtx, MessageStreamContext
msgCtx)
{
- MessageStreamAdapter adapter = endpoint.getAttachment(MessageStreamAdapter.class);
- if(null == adapter)
- throw new IllegalArgumentException("Cannot obtain
MessageStreamAdapter");
-
- try
+ boolean invCtxAvailable = (invCtx != null);
+ boolean invCtxHasProps = (invCtx.getAttachment(Properties.class) != null);
+
+ if (invCtxAvailable && invCtxHasProps)
{
- MessageStreamContext streamContext = new MessageStreamContext();
-
- if(context!=null && context.getAttachment(Properties.class)!=null)
+ Map<String, String> msgReqCtx = msgCtx.getRequestContext();
+ Properties invCtxProps = invCtx.getAttachment(Properties.class);
+
+ // copy invocation properties to message request context
+ for(Object keyObject : invCtxProps.keySet())
{
- Properties props = context.getAttachment(Properties.class);
- for(Object o : props.keySet())
- {
- String key = (String)o;
- streamContext.getRequestContext().put(key, props.getProperty(key));
- }
+ String key = (String)keyObject;
+ msgReqCtx.put(key, invCtxProps.getProperty(key));
}
-
- // Hacky, but the InvokerJSE requires it.
- // However it's bette to do it here then outside the RequestHandler...
- EndpointAssociation.setEndpoint(endpoint);
- try
- {
- adapter.handle(streamContext, inStream, outStream );
- }
- finally
- {
- EndpointAssociation.removeEndpoint();
- }
-
}
- catch (IOException e)
- {
- throw new WebServiceException("Failed to process request: " +
e.getMessage(), e);
-
- }
-
}
- public void handleWSDLRequest(Endpoint endpoint, OutputStream outStream,
InvocationContext context)
+ /**
+ * Sends HTTP text message to the client
+ * @param status HTTP status code to return
+ * @param message text message
+ * @param res response to write message to
+ * @throws IOException when some IO problem occurs
+ */
+ private static void sendResponse(int status, String message, HttpServletResponse res)
+ throws IOException
{
- InputStream inStream = null;
-
- try
- {
- String epAddress = endpoint.getAddress();
- if (epAddress == null)
- throw new IllegalArgumentException("Invalid endpoint address: " +
epAddress);
-
- URL wsdlUrl = new URL(epAddress + "?wsdl");
- inStream = wsdlUrl.openStream();
- IOUtils.copyStream(outStream, inStream);
- }
- catch (IOException e)
- {
- throw new WebServiceException("Failed to process WSDL request: " +
e.getMessage(), e);
- }
- finally
- {
- try
- {
- if(inStream!=null)inStream.close();
- if(outStream!=null)outStream.close();
- }
- catch (IOException e)
- {
- //
- }
- }
+ res.setStatus(status);
+ res.setContentType("text/plain");
+ Writer out = res.getWriter();
+ out.write(message);
+ out.close();
}
/**
* Returns true if the given query string is for metadata request.
- *
- * @param query
- * String like "xsd=1" or
"perhaps=some&unrelated=query".
- * Can be null.
- * @return true for metadata requests
- * false for web service requests
+ * @param query HTTP query, can be null
+ * @return true for metadata requests false otherwise
*/
- private boolean isMetadataQuery(String query) {
- // we intentionally return true even if documents don't exist,
- // so that they get 404.
- return query != null && (query.equals("WSDL") ||
query.startsWith("wsdl") || query.startsWith("xsd="));
+ private static boolean isMetadataQuery(String query)
+ {
+ // we intentionally return true even if documents don't exist, so that they get
404.
+ return (query != null) && (query.equals("WSDL") ||
query.startsWith("wsdl") || query.startsWith("xsd="));
}
}