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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3006 - (show annotations) (download)
Tue Apr 19 15:49:14 2016 UTC (8 years ago) by torben
File size: 4406 byte(s)
1) Load DAWA to local csv file
2) use commons CSV to parse downloaded file
3) read all kommunekoder(muncipality codes) in region from dawa and use this to handle each muncipality individualy (smaller batches)
1 package dk.daoas.adressevedligehold.util;
2
3 // kopieret fra dk.thoerup.genericjavautils;
4
5 import java.io.ByteArrayOutputStream;
6 import java.io.File;
7 import java.io.FileOutputStream;
8 import java.io.IOException;
9 import java.io.InputStream;
10 import java.io.OutputStream;
11 import java.io.UnsupportedEncodingException;
12 import java.net.HttpURLConnection;
13 import java.net.URL;
14 import java.net.URLConnection;
15 import java.net.URLDecoder;
16 import java.net.URLEncoder;
17 import java.nio.charset.Charset;
18 import java.util.HashMap;
19 import java.util.Map;
20
21
22 public class HttpUtil {
23
24 static public Map<String,String> decodeUri(String uri) {
25 if (uri.indexOf('?') != -1) {
26 String parts[] = uri.split("\\?");
27 return decodeParams( parts[1] );
28 } else {
29 return new HashMap<String,String>();
30 }
31
32
33
34 }
35
36 static public Map<String,String> decodeParams(String str) {
37 Map<String,String> res = new HashMap<String,String>();
38
39
40 String parts[] = str.split("&");
41
42 for (String part : parts) {
43 String pair[] = part.split("=");
44 String key = pair[0];
45
46 String val = null;
47 if (pair.length == 2) {
48
49
50 try {
51 val = URLDecoder.decode(pair[1], "ISO-8859-1");
52 } catch (UnsupportedEncodingException e) {
53 val = pair[1]; // if decode fails try with the raw string
54 }
55
56 }
57
58 res.put(key, val);
59 }
60
61 return res;
62 }
63
64 static public String getLastPart(String url) {
65 String parts[] = url.split("/");
66 String lastPart = parts[ parts.length - 1];
67
68 return lastPart;
69 }
70
71 static public String encodeParams(Map<String,String> params) {
72 StringBuilder sb = new StringBuilder();
73
74 boolean isFirst = true;
75 for (Map.Entry<String,String> entry : params.entrySet()) {
76 if (isFirst) {
77 isFirst = false;
78 } else {
79 sb.append("&");
80 }
81
82
83 sb.append( entry.getKey() );
84 sb.append("=");
85 sb.append( encode( entry.getValue() ) );
86
87
88 }
89
90
91 return sb.toString();
92
93 }
94
95 public static String encode(String url) {
96 if (url == null)
97 return "";
98
99 try {
100 return URLEncoder.encode(url, "UTF-8");
101 } catch (Exception e) {
102 return url;
103 }
104 }
105
106 public static String getContentString(String uri, int timeout) throws IOException {
107 return getContentString(uri, timeout, "UTF-8");
108 }
109
110 public static String getContentString(String uri, int timeout, String encoding) throws IOException {
111 byte[] buf = getContent(uri, timeout);
112 return new String(buf, encoding);
113 }
114
115 public static byte[] getContent(String uri, int timeout) throws IOException {
116 URL url = new URL(uri);
117 URLConnection connection = url.openConnection();
118
119 connection.setConnectTimeout(timeout);
120 connection.setReadTimeout(timeout);
121 InputStream is = connection.getInputStream();
122
123 return readInputStream(is);
124 }
125
126 public static void getContentToFile(String uri, File f, int timeout) throws IOException {
127 URL url = new URL(uri);
128 URLConnection connection = url.openConnection();
129
130 connection.setConnectTimeout(timeout);
131 connection.setReadTimeout(timeout);
132 InputStream is = connection.getInputStream();
133
134 saveInputStreamToFile(is,f);
135 }
136
137 public static byte[] postContent(String uri, String data, int timeout) throws IOException {
138 URL url = new URL(uri);
139 HttpURLConnection connection = (HttpURLConnection) url.openConnection();
140 connection.setDoOutput(true);
141 connection.setRequestMethod("POST");
142 connection.setConnectTimeout(timeout);
143
144 OutputStream os = connection.getOutputStream();
145 os.write( data.getBytes(Charset.forName("UTF-8")) );
146
147 InputStream is = connection.getInputStream();
148
149 return readInputStream(is);
150
151 }
152
153 static byte[] readInputStream(InputStream is) throws IOException{
154 byte buffer[] = new byte[64 * 1024];
155 //try /; //start buffer size - instead of default 32bytes
156 try (ByteArrayOutputStream baos = new ByteArrayOutputStream(16*1024*1024)) {
157 int count;
158
159 while ( (count = is.read(buffer)) != -1) {
160 baos.write(buffer, 0, count);
161 }
162 is.close();
163 return baos.toByteArray();
164 }
165 }
166
167 static void saveInputStreamToFile(InputStream is,File fOut) throws IOException{
168 byte buffer[] = new byte[64 * 1024];
169
170 //try /; //start buffer size - instead of default 32bytes
171 try (FileOutputStream fos = new FileOutputStream(fOut) ) {
172 int count;
173
174 while ( (count = is.read(buffer)) != -1) {
175
176 fos.write(buffer, 0, count);
177 }
178 is.close();
179 }
180
181 }
182
183 }

  ViewVC Help
Powered by ViewVC 1.1.20