Home » synapse-1.2-src » org.apache.synapse.transport.nhttp.util » [javadoc | source]

    1   /*
    2    *  Licensed to the Apache Software Foundation (ASF) under one
    3    *  or more contributor license agreements.  See the NOTICE file
    4    *  distributed with this work for additional information
    5    *  regarding copyright ownership.  The ASF licenses this file
    6    *  to you under the Apache License, Version 2.0 (the
    7    *  "License"); you may not use this file except in compliance
    8    *  with the License.  You may obtain a copy of the License at
    9    *
   10    *   http://www.apache.org/licenses/LICENSE-2.0
   11    *
   12    *  Unless required by applicable law or agreed to in writing,
   13    *  software distributed under the License is distributed on an
   14    *   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   15    *  KIND, either express or implied.  See the License for the
   16    *  specific language governing permissions and limitations
   17    *  under the License.
   18    */
   19   
   20   package org.apache.synapse.transport.nhttp.util;
   21   
   22   import org.apache.axiom.om.OMElement;
   23   import org.apache.axis2.AxisFault;
   24   import org.apache.axis2.context.MessageContext;
   25   import org.apache.axis2.description.WSDL20DefaultValueHolder;
   26   import org.apache.axis2.description.WSDL2Constants;
   27   import org.apache.axis2.transport.http.util.URIEncoderDecoder;
   28   
   29   import java.io.UnsupportedEncodingException;
   30   import java.util.Iterator;
   31   
   32   /**
   33    * This class provides a set of utility methods to manage the REST invocation calls
   34    * going out from the nhttp transport in the HTTP GET method
   35    */
   36   public class RESTUtil {
   37   
   38       /**
   39        * This method will return the URI part for the GET HTTPRequest by converting
   40        * the SOAP infoset to the URL-encoded GET format
   41        * 
   42        * @param messageContext - from which the SOAP infoset will be extracted to encode
   43        * @param address        - address of the actual service
   44        * @return uri       - ERI of the GET request
   45        * @throws AxisFault - if the SOAP infoset cannot be converted in to the GET URL-encoded format
   46        */
   47       public static String getURI(MessageContext messageContext, String address) throws AxisFault {
   48           
   49           OMElement firstElement;
   50           address = address.substring(address.indexOf("//") + 2);
   51           address = address.substring(address.indexOf("/"));
   52           String queryParameterSeparator = (String) messageContext
   53                   .getProperty(WSDL2Constants.ATTR_WHTTP_QUERY_PARAMETER_SEPARATOR);
   54           // In case queryParameterSeparator is null we better use the default value
   55   
   56           if (queryParameterSeparator == null) {
   57               queryParameterSeparator = WSDL20DefaultValueHolder
   58                       .getDefaultValue(WSDL2Constants.ATTR_WHTTP_QUERY_PARAMETER_SEPARATOR);
   59           }
   60   
   61           firstElement = messageContext.getEnvelope().getBody().getFirstElement();
   62           String params = "";
   63   
   64           if (firstElement != null) {
   65   
   66               // first element corresponds to the operation name
   67               address = address + "/" + firstElement.getLocalName();
   68   
   69               Iterator iter = firstElement.getChildElements();
   70   
   71               String legalCharacters = WSDL2Constants
   72                   .LEGAL_CHARACTERS_IN_QUERY.replaceAll(queryParameterSeparator, "");
   73               StringBuffer buff = new StringBuffer(params);
   74   
   75               // iterate through the child elements and find the request parameters
   76               while (iter.hasNext()) {
   77                   OMElement element = (OMElement) iter.next();
   78                   try {
   79                       buff.append(URIEncoderDecoder.quoteIllegal(element.getLocalName(),
   80                           legalCharacters)).append("=").append(URIEncoderDecoder.quoteIllegal(element.getText(),
   81                           legalCharacters)).append(queryParameterSeparator);
   82                   } catch (UnsupportedEncodingException e) {
   83                       throw new AxisFault("URI Encoding error : " + element.getLocalName()
   84                           + "=" + element.getText(), e);
   85                   }
   86               }
   87   
   88               params = buff.toString();
   89           } else {
   90               throw new AxisFault("Cannot convert SOAP infoset to a GET URL-encoded format");
   91           }
   92   
   93           if (params.trim().length() != 0) {
   94               int index = address.indexOf("?");
   95               if (index == -1) {
   96                   address = address + "?" + params.substring(0, params.length() - 1);
   97               } else if (index == address.length() - 1) {
   98                   address = address + params.substring(0, params.length() - 1);
   99   
  100               } else {
  101                   address = address
  102                       + queryParameterSeparator + params.substring(0, params.length() - 1);
  103               }
  104           }
  105           
  106           return address;
  107       }
  108   }

Home » synapse-1.2-src » org.apache.synapse.transport.nhttp.util » [javadoc | source]