| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
package groovyx.net.http; |
| 23 | |
|
| 24 | |
import java.net.MalformedURLException; |
| 25 | |
import java.net.URI; |
| 26 | |
import java.net.URISyntaxException; |
| 27 | |
import java.net.URL; |
| 28 | |
import java.util.ArrayList; |
| 29 | |
import java.util.HashMap; |
| 30 | |
import java.util.List; |
| 31 | |
import java.util.Map; |
| 32 | |
|
| 33 | |
import org.apache.http.NameValuePair; |
| 34 | |
import org.apache.http.client.utils.URLEncodedUtils; |
| 35 | |
import org.apache.http.message.BasicNameValuePair; |
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
public class URIBuilder { |
| 60 | |
protected URI base; |
| 61 | 98 | private final String ENC = "UTF-8"; |
| 62 | |
|
| 63 | 12 | public URIBuilder( String url ) throws URISyntaxException { |
| 64 | 12 | base = new URI(url); |
| 65 | 12 | } |
| 66 | |
|
| 67 | 0 | public URIBuilder( URL url ) throws URISyntaxException { |
| 68 | 0 | this.base = url.toURI(); |
| 69 | 0 | } |
| 70 | |
|
| 71 | 86 | public URIBuilder( URI url ) { |
| 72 | 86 | this.base = url; |
| 73 | 86 | } |
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
public static URI convertToURI( Object uri ) throws URISyntaxException { |
| 83 | 63 | if ( uri instanceof URI ) ; |
| 84 | 44 | else if ( uri instanceof URL ) uri = ((URL)uri).toURI(); |
| 85 | 44 | else uri = new URI( uri.toString() ); |
| 86 | 63 | return (URI)uri; |
| 87 | |
} |
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
public URIBuilder setScheme( String scheme ) throws URISyntaxException { |
| 94 | 4 | this.base = new URI( scheme, base.getUserInfo(), |
| 95 | |
base.getHost(), base.getPort(), base.getPath(), |
| 96 | |
base.getQuery(), base.getFragment() ); |
| 97 | 4 | return this; |
| 98 | |
} |
| 99 | |
|
| 100 | |
public URIBuilder setPort( int port ) throws URISyntaxException { |
| 101 | 4 | this.base = new URI( base.getScheme(), base.getUserInfo(), |
| 102 | |
base.getHost(), port, base.getPath(), |
| 103 | |
base.getQuery(), base.getFragment() ); |
| 104 | 4 | return this; |
| 105 | |
} |
| 106 | |
|
| 107 | |
public URIBuilder setHost( String host ) throws URISyntaxException { |
| 108 | 4 | this.base = new URI( base.getScheme(), base.getUserInfo(), |
| 109 | |
host, base.getPort(), base.getPath(), |
| 110 | |
base.getQuery(), base.getFragment() ); |
| 111 | 4 | return this; |
| 112 | |
} |
| 113 | |
|
| 114 | |
public URIBuilder setPath( String path ) throws URISyntaxException { |
| 115 | 63 | path = base.resolve( path ).getPath(); |
| 116 | 63 | this.base = new URI( base.getScheme(), base.getUserInfo(), |
| 117 | |
base.getHost(), base.getPort(), path, |
| 118 | |
base.getQuery(), base.getFragment() ); |
| 119 | 63 | return this; |
| 120 | |
} |
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
public URIBuilder setQuery( Map<String,?> params ) throws URISyntaxException { |
| 129 | 12 | List<NameValuePair> pairs = new ArrayList<NameValuePair>(params.size()); |
| 130 | 12 | for ( Map.Entry<String, ?> entry : params.entrySet() ) { |
| 131 | 36 | String val = ( entry.getValue() != null ) ? |
| 132 | |
entry.getValue().toString() : ""; |
| 133 | 36 | pairs.add( new BasicNameValuePair( |
| 134 | |
entry.getKey(), val ) ); |
| 135 | 36 | } |
| 136 | 12 | String queryString = URLEncodedUtils.format( pairs, ENC ); |
| 137 | 12 | this.base = new URI( base.getScheme(), base.getUserInfo(), |
| 138 | |
base.getHost(), base.getPort(), base.getPath(), |
| 139 | |
queryString, base.getFragment() ); |
| 140 | 12 | return this; |
| 141 | |
} |
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
public Map<String,String> getQuery() { |
| 148 | 24 | Map<String,String> params = new HashMap<String, String>(); |
| 149 | 24 | List<NameValuePair> pairs = URLEncodedUtils.parse( this.base, ENC ); |
| 150 | 24 | for ( NameValuePair pair : pairs ) |
| 151 | 76 | params.put( pair.getName(), pair.getValue() ); |
| 152 | 24 | return params; |
| 153 | |
} |
| 154 | |
|
| 155 | |
public boolean hasQueryParam( String name ) { |
| 156 | 0 | return getQuery().get( name ) != null; |
| 157 | |
} |
| 158 | |
|
| 159 | |
public URIBuilder removeQueryParam( String param ) throws URISyntaxException { |
| 160 | 0 | Map<String,String> params = getQuery(); |
| 161 | 0 | params.remove( param ); |
| 162 | 0 | this.setQuery( params ); |
| 163 | 0 | return this; |
| 164 | |
} |
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
public URIBuilder addQueryParam( String param, Object value ) throws URISyntaxException { |
| 174 | 4 | Map<String,String> params = getQuery(); |
| 175 | 4 | if ( value == null ) value = ""; |
| 176 | 4 | params.put( param, value.toString() ); |
| 177 | 4 | this.setQuery( params ); |
| 178 | 4 | return this; |
| 179 | |
} |
| 180 | |
|
| 181 | |
@SuppressWarnings("unchecked") |
| 182 | |
public URIBuilder addQueryParams( Map<String,?> params ) throws URISyntaxException { |
| 183 | 0 | Map existing = this.getQuery(); |
| 184 | 0 | existing.putAll( params ); |
| 185 | 0 | this.setQuery( existing ); |
| 186 | 0 | return this; |
| 187 | |
} |
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
public URIBuilder setFragment( String fragment ) throws URISyntaxException { |
| 195 | 4 | this.base = new URI( base.getScheme(), base.getUserInfo(), |
| 196 | |
base.getHost(), base.getPort(), base.getPath(), |
| 197 | |
base.getQuery(), fragment ); |
| 198 | 4 | return this; |
| 199 | |
} |
| 200 | |
|
| 201 | |
@Override public String toString() { |
| 202 | 40 | return base.toString(); |
| 203 | |
} |
| 204 | |
|
| 205 | |
public URL toURL() throws MalformedURLException { |
| 206 | 0 | return base.toURL(); |
| 207 | |
} |
| 208 | |
|
| 209 | 59 | public URI toURI() { return this.base; } |
| 210 | |
} |