Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2006, Red Hat Middleware LLC, and individual contributors
   * as indicated by the @author tags. See the copyright.txt file in the
   * distribution for a full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 package org.jboss.wsf.stack.metro;
 
 
 import java.io.Writer;
 import java.util.Map;
 import java.net.URL;

Request handler that delegates to Metro's ServletAdapter.

Author(s):
Thomas.Diesler@jboss.org
Since:
25-Apr-2007
 
 class RequestHandlerImpl implements RequestHandler
 {
    // provide logging
    private static final Logger log = Logger.getLogger(RequestHandlerImpl.class);
 
    {
    }

   
Handles HTTP requests. It supports POST and GET HTTP methods only.

Parameters:
endpoint endpoint
req servlet request to handle
res servlet response to return
servletCtx servlet context
Throws:
javax.servlet.ServletException when some problem occurs
java.io.IOException when some IO problem occurs
 
    public void handleHttpRequest(Endpoint endpointHttpServletRequest reqHttpServletResponse resServletContext servletCtx)
    {
       ServletAdapter target = endpoint.getAttachment(ServletAdapter.class);
       if(null == target)
          throw new IllegalStateException("Cannot obtain ServletAdapter");
 
       EndpointAssociation.setEndpoint(endpoint);
       try
       {
          String method = req.getMethod();
          if (method.equals("POST"))
          {
             doPost(targetservletCtxreqres);
          }
          else if(method.equals("GET"))
          {
             doGet(targetreqservletCtxres);
          }
          else
          {
             throw new WebServiceException("Unsupported method: " + method);
          }
       }
       finally
       {
          EndpointAssociation.removeEndpoint();
       }
    }

   
The InvocationContext accepts a java.util.Properties attachment that can carry request properties.
The properties keys are derived from MessageStreamContext

Parameters:
endpoint endpoint
inStream input stream
outStream output stream
invCtx invocation context
   public void handleRequest(Endpoint endpointInputStream inStreamOutputStream outStreamInvocationContext invCtx)
   {
      MessageStreamAdapter adapter = endpoint.getAttachment(MessageStreamAdapter.class);
      if (adapter == null)
         throw new IllegalStateException("Cannot obtain: " + adapter);
      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(invCtxstreamContext);
         adapter.handle(streamContextinStreamoutStream );
      }
      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 ?wsdl query string. The constructed URL is used to create the input stream to read WSDL content from and submited to the user.

Parameters:
endpoint endpoint
outStream output stream
invCtx invocation context
   public void handleWSDLRequest(Endpoint endpointOutputStream outStreamInvocationContext 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(outStreaminStream);
      }
      catch (IOException e)
      {
         throw new WebServiceException("Failed to process WSDL request: " + e.getMessage(), e);
      }
      finally
      {
         // close input stream when available
         try
         {
            if(inStream!=nullinStream.close();
         }
         catch (IOException ignore) {}
         // close output stream when available
         try
         {
            if(outStream!=nulloutStream.close();
         }
         catch (IOException ignore) {}
      }
   }

   
Handles HTTP GET request using Metro's ServletAdapter publishWSDL method

Parameters:
target Metro's ServletAdapter
req request message
context servlet context
res response message
Throws:
javax.servlet.ServletException if some problem occurs
   private static void doGet(ServletAdapter targetHttpServletRequest reqServletContext contextHttpServletResponse res)
   throws ServletException
   {
      try
      {
         if (target != null)
         {
            String query = req.getQueryString();
            if (isMetadataQuery(query))
            {
               // Sends published WSDL and schema documents
               target.publishWSDL(contextreqres);
               return;
            }
            else
            {
               sendResponse(405, "HTTP GET not supported"res);
            }
         }
         else
         {
            sendResponse(404, "Not found"res);
         }
      }
      catch (Exception e)
      {
         .error("Failed to process GET request"e);
         throw new ServletException(e.getMessage());
      }
   }

   
Handles HTTP POST request using Metro's ServletAdapter handle method

Parameters:
target Metro's ServletAdapter
req request message
context servlet context
res response message
Throws:
javax.servlet.ServletException if some problem occurs
   private static void doPost(ServletAdapter targetServletContext contextHttpServletRequest reqHttpServletResponse res)
   {
      try
      {
         target.handle(contextreqres);
      }
      catch (Throwable e)
      {
         .error("Failed to process POST request"e);
      }
   }
   
   
Copies properties from invocation context to message context when properties are available

Parameters:
invCtx invocation context
msgCtx message context
   private static void copyProperties(InvocationContext invCtxMessageStreamContext msgCtx)
   {
      boolean invCtxHasProps = (invCtx != null) && (invCtx.getAttachment(Properties.class) != null);
      
      if (invCtxHasProps)
      {
         Map<StringStringmsgReqCtx = msgCtx.getRequestContext(); 
         Properties invCtxProps = invCtx.getAttachment(Properties.class);
         
         // copy invocation properties to message request context
         for(Object keyObject : invCtxProps.keySet())
         {
            String key = (String)keyObject;
            msgReqCtx.put(keyinvCtxProps.getProperty(key));
         }
      }
   }

   
Sends HTTP text message to the client

Parameters:
status HTTP status code to return
message text message
res response to write message to
Throws:
java.io.IOException when some IO problem occurs
   private static void sendResponse(int statusString messageHttpServletResponse res)
   throws IOException
   {
      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.

Parameters:
query HTTP query, can be null
Returns:
true for metadata requests false otherwise
   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="));
   }
New to GrepCode? Check out our FAQ X