// callRestfulApi - Calls restful API and returns results as a string public String callRestfulApi( String addr, HttpServletRequest request, HttpServletResponse response) { if (localCookie) CookieHandler.setDefault(cm); try { ByteArrayOutputStream output = new ByteArrayOutputStream(); URL url = new URL(API_ROOT + addr); URLConnection urlConnection = url.openConnection(); String cookieVal = getBrowserInfiniteCookie(request); if (cookieVal != null) { urlConnection.addRequestProperty("Cookie", "infinitecookie=" + cookieVal); urlConnection.setDoInput(true); urlConnection.setDoOutput(true); urlConnection.setRequestProperty("Accept-Charset", "UTF-8"); } IOUtils.copy(urlConnection.getInputStream(), output); String newCookie = getConnectionInfiniteCookie(urlConnection); if (newCookie != null && response != null) { setBrowserInfiniteCookie(response, newCookie, request.getServerPort()); } return output.toString(); } catch (IOException e) { System.out.println(e.getMessage()); return null; } } // TESTED
public InputStream getResourceAsStream(String path) { URL url = getResource(path); if (url == null) return null; try { return url.openStream(); } catch (IOException ioe) { throw new RuntimeException("can't getResourceAsStream [" + path + "]", ioe); } }
// Strip off everything unneeded private String getBaseUrl(String pUrl, String pServletPath) { String sUrl; try { URL url = new URL(pUrl); String host = getIpIfPossible(url.getHost()); sUrl = new URL(url.getProtocol(), host, url.getPort(), pServletPath).toExternalForm(); } catch (MalformedURLException exp) { sUrl = plainReplacement(pUrl, pServletPath); } return sUrl; }
String srvUrlStem(String host) { if (host == null) { return null; } StringBuilder sb = new StringBuilder(); sb.append(reqURL.getProtocol()); sb.append("://"); sb.append(host); sb.append(':'); sb.append(reqURL.getPort()); return sb.toString(); }
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html"); PrintWriter out = res.getWriter(); int i, j; String amount = req.getParameter("amount"); String from = req.getParameter("from"); String to = req.getParameter("to"); String ch = req.getParameter("choice"); String answer, answer1, answer2, answer3; if ("INFO".equals(ch)) { Redirect_info newurl = (Redirect_info) getServletContext().getAttribute("redirect"); res.sendRedirect(newurl.getUrl()); } else { out.println("<html>"); out.println("<title>Currency Converter</title>"); String addr = "http://www.google.com/ig/calculator?hl=en&q=" + amount + from + "=?" + to; URL convert = new URL(addr); BufferedReader in = new BufferedReader(new InputStreamReader(convert.openStream())); answer = in.readLine(); answer = new String(answer.getBytes("ISO-8859-1"), "ISO-8859-7"); from = new String(from.getBytes("ISO-8859-1"), "ISO-8859-7"); to = new String(to.getBytes("ISO-8859-1"), "ISO-8859-7"); amount = new String(amount.getBytes("ISO-8859-1"), "ISO-8859-7"); in.close(); i = answer.indexOf('"'); answer = answer.substring(i + 1); i = answer.indexOf('"'); answer = answer.substring(i + 1); i = answer.indexOf('"'); answer = answer.substring(i + 1); i = answer.indexOf('"'); answer = answer.substring(0, i); out.println("<p ALIGN=CENTER>" + amount + " " + from + " == " + answer + "(" + to + ")</p>"); out.println("</body>"); out.println("</html>"); } }
String srvURL(PeerIdentity peer, ServletDescr d, String params) { return srvURLFromStem(peer.getUiUrlStem(reqURL.getPort()), d, params); }
String getRequestHost() { return reqURL.getHost(); }