1
2
3
4
5
6
7
8
9
10
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.MalformedURLException;
23 import java.net.URL;
24 import java.net.URLDecoder;
25
26 public class RestUtils
27 {
28 public static String[] extractTemplateParams( String path )
29 {
30 if( StringUtils.isNullOrEmpty( path ) )
31 return new String[0];
32
33 StringList result = new StringList();
34
35 int ix = path.indexOf( '{' );
36 while( ix != -1 )
37 {
38 int endIx = path.indexOf( '}', ix );
39 if( endIx == -1 )
40 break;
41
42 if( endIx > ix + 1 )
43 result.add( path.substring( ix + 1, endIx ) );
44
45 ix = path.indexOf( '{', ix + 1 );
46 }
47
48 return result.toStringArray();
49
50 }
51
52 public static String extractParams( String pathOrEndpoint, XmlBeansRestParamsTestPropertyHolder params, boolean keepHost )
53 {
54 if( StringUtils.isNullOrEmpty( pathOrEndpoint ) )
55 return "";
56
57 String path = pathOrEndpoint;
58 String queryString = "";
59 URL url = null;
60
61 try
62 {
63 url = new URL( pathOrEndpoint );
64 path = url.getPath();
65 queryString = url.getQuery();
66 }
67 catch( MalformedURLException e )
68 {
69 int ix = path.indexOf( '?' );
70 if( ix >= 0 )
71 {
72 queryString = path.substring( ix + 1 );
73 path = path.substring( 0, ix );
74 }
75 }
76
77 String[] items = path.split( "/" );
78
79 int templateParamCount = 0;
80 StringBuffer resultPath = new StringBuffer();
81
82 for( int i = 0; i < items.length; i++ )
83 {
84 String item = items[i];
85 try
86 {
87 if( item.startsWith( "{" ) && item.endsWith( "}" ) )
88 {
89 String name = item.substring( 1, item.length() - 1 );
90 RestParamProperty property = params.getProperty( name );
91 if( !params.hasProperty( name ) )
92 {
93 property = params.addProperty( name );
94 }
95
96 property.setStyle( ParameterStyle.TEMPLATE );
97 property.setValue( name );
98 }
99 else
100 {
101 String[] matrixParams = item.split( ";" );
102 if( matrixParams.length > 0 )
103 {
104 item = matrixParams[0];
105 for( int c = 1; c < matrixParams.length; c++ )
106 {
107 String matrixParam = matrixParams[c];
108
109 int ix = matrixParam.indexOf( '=' );
110 if( ix == -1 )
111 {
112 String name = URLDecoder.decode( matrixParam, "Utf-8" );
113 if( !params.hasProperty( name ) )
114 params.addProperty( name ).setStyle( ParameterStyle.MATRIX );
115 }
116 else
117 {
118
119 String name = URLDecoder.decode( matrixParam.substring( 0, ix ), "Utf-8" );
120 RestParamProperty property = params.getProperty( name );
121 if( property == null )
122 {
123 property = params.addProperty( name );
124 }
125
126 property.setStyle( ParameterStyle.MATRIX );
127 property.setValue( URLDecoder.decode( matrixParam.substring( ix + 1 ), "Utf-8" ) );
128 }
129 }
130 }
131
132 Integer.parseInt( item );
133
134 String name = "param" + templateParamCount++;
135 RestParamProperty property = params.getProperty( name );
136 if( !params.hasProperty( name ) )
137 {
138 property = params.addProperty( name );
139 }
140
141 property.setStyle( ParameterStyle.TEMPLATE );
142 property.setValue( item );
143
144 item = "{" + property.getName() + "}";
145 }
146 }
147 catch( Exception e )
148 {
149 }
150
151 if( StringUtils.hasContent( item ) )
152 resultPath.append( '/' ).append( item );
153 }
154
155 if( StringUtils.hasContent( queryString ) )
156 {
157 items = queryString.split( "&" );
158 for( String item : items )
159 {
160 try
161 {
162 int ix = item.indexOf( '=' );
163 if( ix == -1 )
164 {
165 String name = URLDecoder.decode( item, "Utf-8" );
166
167 if( !params.hasProperty( name ) )
168 {
169 params.addProperty( name ).setStyle( ParameterStyle.QUERY );
170 }
171 }
172 else
173 {
174 String name = URLDecoder.decode( item.substring( 0, ix ), "Utf-8" );
175 RestParamProperty property = params.getProperty( name );
176 if( property == null )
177 {
178 property = params.addProperty( name );
179 }
180
181 property.setStyle( ParameterStyle.QUERY );
182 property.setValue( URLDecoder.decode( item.substring( ix + 1 ), "Utf-8" ) );
183 }
184 }
185 catch( UnsupportedEncodingException e )
186 {
187 e.printStackTrace();
188 }
189 }
190 }
191
192 if( keepHost && url != null )
193 {
194 return Tools.getEndpointFromUrl( url ) + resultPath.toString();
195 }
196
197 return resultPath.toString();
198 }
199 }