1
2
3
4
5
6 package net.kwfgrid.gwui.servlets;
7
8 import java.io.*;
9 import java.net.*;
10 import javax.servlet.http.*;
11
12 import org.apache.log4j.Logger;
13
14 public class ServletUtilities {
15 private static final Logger logger = Logger.getLogger(ServletUtilities.class);
16
17 public static final String readFile(String urlspec) throws IOException {
18 logger.info("Downloading file from "+urlspec);
19
20 URL url = null;
21 try {
22 url = new URL( urlspec );
23 } catch (MalformedURLException x) {
24 IOException iox = new IOException("Malformed URL.");
25 iox.initCause(x);
26 throw iox;
27 }
28
29 InputStream in = url.openConnection().getInputStream();
30 BufferedReader reader = new BufferedReader(new InputStreamReader(in));
31 String line = null;
32 StringBuffer form = new StringBuffer();
33
34 while ((line = reader.readLine()) != null) {
35 form.append(line).append("\n");
36 }
37
38 return form.toString();
39 }
40 public static final String decode(String encoded) throws UnsupportedEncodingException {
41 if (encoded == null) return null;
42 return URLDecoder.decode(encoded, "UTF-8" );
43 }
44 public static final String replace(String form, String placeholder, String value) {
45 logger.debug("Replacing "+placeholder+" with "+value+" in \n"+form);
46 return form.replaceAll( placeholder, value);
47 }
48 public static final void printHeadWithTitle(String title, PrintWriter out) {
49 out.println("<head><title>"+title+"</title></head>\n");
50 }
51
52 }