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.support.XmlBeansRestParamsTestPropertyHolder.ParameterStyle;
16  import com.eviware.soapui.impl.rest.support.XmlBeansRestParamsTestPropertyHolder.RestParamProperty;
17  import com.eviware.soapui.support.StringUtils;
18  import com.eviware.soapui.support.Tools;
19  import com.eviware.soapui.support.types.StringList;
20  
21  import java.io.UnsupportedEncodingException;
22  import java.net.URL;
23  import java.net.URLDecoder;
24  
25  public class RestUtils
26  {
27     public static String[] extractTemplateParams( String path )
28     {
29        if( StringUtils.isNullOrEmpty( path ))
30           return new String[0];
31  
32        StringList result = new StringList();
33  
34        int ix = path.indexOf( '{' );
35        while( ix != -1 )
36        {
37           int endIx = path.indexOf( '}', ix );
38           if( endIx == -1 )
39              break;
40  
41           if( endIx > ix + 1 )
42              result.add( path.substring( ix + 1, endIx ) );
43  
44           ix = path.indexOf( '{', ix + 1 );
45        }
46  
47        return result.toStringArray();
48  
49     }
50  
51     public static String extractParams( URL param, XmlBeansRestParamsTestPropertyHolder params, boolean keepHost )
52     {
53        if( param == null || StringUtils.isNullOrEmpty( param.getPath() ))
54           return "";
55  
56        String path = param.getPath();
57        String[] items = path.split( "/" );
58  
59        int templateParamCount = 0;
60        StringBuffer resultPath = new StringBuffer();
61  
62        for( int i = 0; i < items.length; i++ )
63        {
64           String item = items[i];
65           try
66           {
67              String[] matrixParams = item.split( ";" );
68              if( matrixParams.length > 0 )
69              {
70                 item = matrixParams[0];
71                 for( int c = 1; c < matrixParams.length; c++ )
72                 {
73                    String matrixParam = matrixParams[c];
74  
75                    int ix = matrixParam.indexOf( '=' );
76                    if( ix == -1 )
77                    {
78                       String name = URLDecoder.decode( matrixParam, "Utf-8" );
79                       if( !params.hasProperty( name ) )
80                          params.addProperty( name ).setStyle( ParameterStyle.MATRIX );
81                    }
82                    else
83                    {
84  
85                       String name = URLDecoder.decode( matrixParam.substring( 0, ix ), "Utf-8" );
86                       RestParamProperty property = params.getProperty( name );
87                       if( property == null )
88                       {
89                          property = params.addProperty( name );
90                       }
91  
92                       property.setStyle( ParameterStyle.MATRIX );
93                       property.setValue( URLDecoder.decode( matrixParam.substring( ix + 1 ), "Utf-8" ) );
94                    }
95                 }
96              }
97  
98              Integer.parseInt( item );
99  
100             String name = "param" + templateParamCount++;
101             RestParamProperty property = params.getProperty( name );
102             if( !params.hasProperty( name ) )
103             {
104                property = params.addProperty( name );
105             }
106 
107             property.setStyle( ParameterStyle.TEMPLATE );
108             property.setValue( item );
109 
110             item = "{" + property.getName() + "}";
111          }
112          catch( Exception e )
113          {
114          }
115 
116          if( StringUtils.hasContent( item ) )
117             resultPath.append( '/' ).append( item );
118       }
119 
120       String query = param.getQuery();
121       if( StringUtils.hasContent( query ) )
122       {
123          items = query.split( "&" );
124          for( String item : items )
125          {
126             try
127             {
128                int ix = item.indexOf( '=' );
129                if( ix == -1 )
130                {
131                   String name = URLDecoder.decode( item, "Utf-8" );
132 
133                   if( !params.hasProperty( name ) )
134                   {
135                      params.addProperty( name ).setStyle( ParameterStyle.QUERY );
136                   }
137                }
138                else
139                {
140                   String name = URLDecoder.decode( item.substring( 0, ix ), "Utf-8" );
141                   RestParamProperty property = params.getProperty( name );
142                   if( property == null )
143                   {
144                      property = params.addProperty( name );
145                   }
146 
147                   property.setStyle( ParameterStyle.QUERY );
148                   property.setValue( URLDecoder.decode( item.substring( ix + 1 ), "Utf-8" ) );
149                }
150             }
151             catch( UnsupportedEncodingException e )
152             {
153                e.printStackTrace();
154             }
155          }
156       }
157 
158       if( keepHost )
159       {
160          return Tools.getEndpointFromUrl( param ) + resultPath.toString();
161       }
162 
163       return resultPath.toString();
164    }
165 }