View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 eviware.com
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10   *  See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  
13  package com.eviware.soapui.impl.rest.support;
14  
15  import com.eviware.soapui.impl.rest.RestRequest;
16  import com.eviware.soapui.impl.rest.support.XmlBeansRestParamsTestPropertyHolder.ParameterStyle;
17  import com.eviware.soapui.impl.rest.support.XmlBeansRestParamsTestPropertyHolder.RestParamProperty;
18  import com.eviware.soapui.model.propertyexpansion.DefaultPropertyExpansionContext;
19  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionUtils;
20  import com.eviware.soapui.support.StringUtils;
21  import com.eviware.soapui.support.Tools;
22  import com.eviware.soapui.support.types.StringList;
23  import org.apache.xmlbeans.XmlBoolean;
24  
25  import java.io.UnsupportedEncodingException;
26  import java.net.MalformedURLException;
27  import java.net.URL;
28  import java.net.URLDecoder;
29  import java.net.URLEncoder;
30  
31  public class RestUtils
32  {
33     public static String[] extractTemplateParams( String path )
34     {
35        if( StringUtils.isNullOrEmpty( path ) )
36           return new String[0];
37  
38        StringList result = new StringList();
39  
40        int ix = path.indexOf( '{' );
41        while( ix != -1 )
42        {
43           int endIx = path.indexOf( '}', ix );
44           if( endIx == -1 )
45              break;
46  
47           if( endIx > ix + 1 )
48              result.add( path.substring( ix + 1, endIx ) );
49  
50           ix = path.indexOf( '{', ix + 1 );
51        }
52  
53        return result.toStringArray();
54  
55     }
56  
57     public static String extractParams( String pathOrEndpoint, XmlBeansRestParamsTestPropertyHolder params, boolean keepHost )
58     {
59        if( StringUtils.isNullOrEmpty( pathOrEndpoint ) )
60           return "";
61  
62        String path = pathOrEndpoint;
63        String queryString = "";
64        URL url = null;
65  
66        try
67        {
68           url = new URL( pathOrEndpoint );
69           path = url.getPath();
70           queryString = url.getQuery();
71        }
72        catch( MalformedURLException e )
73        {
74           int ix = path.indexOf( '?' );
75           if( ix >= 0 )
76           {
77              queryString = path.substring( ix + 1 );
78              path = path.substring( 0, ix );
79           }
80        }
81  
82        String[] items = path.split( "/" );
83  
84        int templateParamCount = 0;
85        StringBuffer resultPath = new StringBuffer();
86  
87        for( int i = 0; i < items.length; i++ )
88        {
89           String item = items[i];
90           try
91           {
92              if( item.startsWith( "{" ) && item.endsWith( "}" ) )
93              {
94                 String name = item.substring( 1, item.length() - 1 );
95                 RestParamProperty property = params.getProperty( name );
96                 if( !params.hasProperty( name ) )
97                 {
98                    property = params.addProperty( name );
99                 }
100 
101                property.setStyle( ParameterStyle.TEMPLATE );
102                property.setValue( name );
103             }
104             else
105             {
106                String[] matrixParams = item.split( ";" );
107                if( matrixParams.length > 0 )
108                {
109                   item = matrixParams[0];
110                   for( int c = 1; c < matrixParams.length; c++ )
111                   {
112                      String matrixParam = matrixParams[c];
113 
114                      int ix = matrixParam.indexOf( '=' );
115                      if( ix == -1 )
116                      {
117                         String name = URLDecoder.decode( matrixParam, "Utf-8" );
118                         if( !params.hasProperty( name ) )
119                            params.addProperty( name ).setStyle( ParameterStyle.MATRIX );
120                      }
121                      else
122                      {
123 
124                         String name = URLDecoder.decode( matrixParam.substring( 0, ix ), "Utf-8" );
125                         RestParamProperty property = params.getProperty( name );
126                         if( property == null )
127                         {
128                            property = params.addProperty( name );
129                         }
130 
131                         property.setStyle( ParameterStyle.MATRIX );
132                         property.setValue( URLDecoder.decode( matrixParam.substring( ix + 1 ), "Utf-8" ) );
133                      }
134                   }
135                }
136 
137                Integer.parseInt( item );
138 
139                String name = "param" + templateParamCount++;
140                RestParamProperty property = params.getProperty( name );
141                if( !params.hasProperty( name ) )
142                {
143                   property = params.addProperty( name );
144                }
145 
146                property.setStyle( ParameterStyle.TEMPLATE );
147                property.setValue( item );
148 
149                item = "{" + property.getName() + "}";
150             }
151          }
152          catch( Exception e )
153          {
154          }
155 
156          if( StringUtils.hasContent( item ) )
157             resultPath.append( '/' ).append( item );
158       }
159 
160       if( StringUtils.hasContent( queryString ) )
161       {
162          items = queryString.split( "&" );
163          for( String item : items )
164          {
165             try
166             {
167                int ix = item.indexOf( '=' );
168                if( ix == -1 )
169                {
170                   String name = URLDecoder.decode( item, "Utf-8" );
171 
172                   if( !params.hasProperty( name ) )
173                   {
174                      params.addProperty( name ).setStyle( ParameterStyle.QUERY );
175                   }
176                }
177                else
178                {
179                   String name = URLDecoder.decode( item.substring( 0, ix ), "Utf-8" );
180                   RestParamProperty property = params.getProperty( name );
181                   if( property == null )
182                   {
183                      property = params.addProperty( name );
184                   }
185 
186                   property.setStyle( ParameterStyle.QUERY );
187                   property.setValue( URLDecoder.decode( item.substring( ix + 1 ), "Utf-8" ) );
188                }
189             }
190             catch( UnsupportedEncodingException e )
191             {
192                e.printStackTrace();
193             }
194          }
195       }
196 
197       if( keepHost && url != null )
198       {
199          return Tools.getEndpointFromUrl( url ) + resultPath.toString();
200       }
201 
202       return resultPath.toString();
203    }
204 
205    public static String expandPath( String path, XmlBeansRestParamsTestPropertyHolder params, RestRequest request )
206    {
207       StringBuffer query = request.isPostQueryString() || "multipart/form-data".equals( request.getMediaType()) ? null : new StringBuffer();
208       DefaultPropertyExpansionContext context = new DefaultPropertyExpansionContext( request );
209 
210       for( int c = 0; c < params.getPropertyCount(); c++ )
211       {
212          RestParamProperty param = params.getPropertyAt( c );
213 
214          String value = PropertyExpansionUtils.expandProperties( context, param.getValue() );
215          if( value != null && !param.isDisableUrlEncoding() )
216             value = URLEncoder.encode( value );
217 
218          if( !StringUtils.hasContent( value ) && !param.getRequired() )
219             continue;
220 
221          switch( param.getStyle() )
222          {
223             case QUERY:
224                if( query != null )
225                {
226                   if( query.length() > 0 )
227                      query.append( '&' );
228 
229                   query.append( URLEncoder.encode( param.getName() ) );
230                   query.append( '=' );
231 
232                   if( StringUtils.hasContent( value ) )
233                      query.append( value );
234                }
235                break;
236             case TEMPLATE:
237                path = path.replaceAll( "//{" + param.getName() + "//}", value );
238                break;
239             case MATRIX:
240                if( param.getType().equals( XmlBoolean.type.getName() ) )
241                {
242                   if( value.toUpperCase().equals( "TRUE" ) || value.equals( "1" ) )
243                   {
244                      path += ";" + param.getName();
245                   }
246                }
247                else
248                {
249                   path += ";" + param.getName();
250                   if( StringUtils.hasContent( value ) )
251                   {
252                      path += "=" + value;
253                   }
254                }
255             case PLAIN:
256                break;
257          }
258       }
259 
260       if( query != null && query.length() > 0 )
261          path += "?" + query.toString();
262 
263       return path;
264    }
265 }