/[projects]/dao/DaoAdresseService/src/main/java/dk/daoas/daoadresseservice/util/HttpUtil.java
ViewVC logotype

Annotation of /dao/DaoAdresseService/src/main/java/dk/daoas/daoadresseservice/util/HttpUtil.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2531 - (hide annotations) (download)
Fri May 8 20:25:12 2015 UTC (9 years, 1 month ago) by torben
File size: 3560 byte(s)
findbugs
1 torben 2321 package dk.daoas.daoadresseservice.util;
2 torben 2301
3     // kopieret fra dk.thoerup.genericjavautils;
4    
5     import java.io.ByteArrayOutputStream;
6     import java.io.IOException;
7     import java.io.InputStream;
8     import java.io.OutputStream;
9     import java.io.UnsupportedEncodingException;
10     import java.net.HttpURLConnection;
11     import java.net.URL;
12     import java.net.URLConnection;
13     import java.net.URLDecoder;
14     import java.net.URLEncoder;
15     import java.util.HashMap;
16     import java.util.Map;
17    
18    
19     public class HttpUtil {
20    
21     static public Map<String,String> decodeUri(String uri) {
22     if (uri.indexOf('?') != -1) {
23     String parts[] = uri.split("\\?");
24     return decodeParams( parts[1] );
25     } else {
26     return new HashMap<String,String>();
27     }
28    
29    
30    
31     }
32    
33     static public Map<String,String> decodeParams(String str) {
34     Map<String,String> res = new HashMap<String,String>();
35    
36    
37     String parts[] = str.split("&");
38    
39     for (String part : parts) {
40     String pair[] = part.split("=");
41     String key = pair[0];
42    
43     String val = null;
44     if (pair.length == 2) {
45    
46    
47     try {
48     val = URLDecoder.decode(pair[1], "ISO-8859-1");
49     } catch (UnsupportedEncodingException e) {
50     val = pair[1]; // if decode fails try with the raw string
51     }
52    
53     }
54    
55     res.put(key, val);
56     }
57    
58     return res;
59     }
60    
61     static public String getLastPart(String url) {
62     String parts[] = url.split("/");
63     String lastPart = parts[ parts.length - 1];
64    
65     return lastPart;
66     }
67    
68     static public String encodeParams(Map<String,String> params) {
69     StringBuilder sb = new StringBuilder();
70    
71     boolean isFirst = true;
72 torben 2531 for (Map.Entry<String,String> entry : params.entrySet()) {
73 torben 2301 if (isFirst) {
74     isFirst = false;
75     } else {
76     sb.append("&");
77     }
78    
79 torben 2531
80     sb.append( entry.getKey() );
81 torben 2301 sb.append("=");
82 torben 2531 sb.append( encode( entry.getValue() ) );
83 torben 2301
84    
85     }
86    
87    
88     return sb.toString();
89    
90     }
91    
92     static String encode(String url) {
93     if (url == null)
94     return "";
95    
96     try {
97     return URLEncoder.encode(url, "UTF-8");
98     } catch (Exception e) {
99     return url;
100     }
101     }
102    
103     public static String getContentString(String uri, int timeout) throws IOException {
104     return getContentString(uri, timeout, "UTF-8");
105     }
106    
107     public static String getContentString(String uri, int timeout, String encoding) throws IOException {
108     byte[] buf = getContent(uri, timeout);
109     return new String(buf, encoding);
110     }
111    
112     public static byte[] getContent(String uri, int timeout) throws IOException {
113     URL url = new URL(uri);
114     URLConnection connection = url.openConnection();
115    
116     connection.setConnectTimeout(timeout);
117 torben 2366 connection.setReadTimeout(timeout);
118 torben 2301 InputStream is = connection.getInputStream();
119    
120     return readInputStream(is);
121     }
122    
123     public static byte[] postContent(String uri, String data, int timeout) throws IOException {
124     URL url = new URL(uri);
125     HttpURLConnection connection = (HttpURLConnection) url.openConnection();
126     connection.setDoOutput(true);
127     connection.setRequestMethod("POST");
128     connection.setConnectTimeout(timeout);
129    
130     OutputStream os = connection.getOutputStream();
131     os.write( data.getBytes() );
132    
133     InputStream is = connection.getInputStream();
134    
135     return readInputStream(is);
136    
137     }
138    
139     static byte[] readInputStream(InputStream is) throws IOException{
140     byte buffer[] = new byte[1024];
141 torben 2326 //try /; //start buffer size - instead of default 32bytes
142     try (ByteArrayOutputStream baos = new ByteArrayOutputStream(32768)) {
143 torben 2301 int count;
144 torben 2326
145 torben 2301 while ( (count = is.read(buffer)) != -1) {
146     baos.write(buffer, 0, count);
147     }
148 torben 2326 is.close();
149     return baos.toByteArray();
150     }
151 torben 2301 }
152    
153     }

  ViewVC Help
Powered by ViewVC 1.1.20