/[projects]/dao/DaoAdresseVedligehold/src/main/java/dk/daoas/adressevedligehold/util/HttpUtil.java
ViewVC logotype

Annotation of /dao/DaoAdresseVedligehold/src/main/java/dk/daoas/adressevedligehold/util/HttpUtil.java

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20