// // The code in the next two methods is based on the code in HttpUtils.java // from // javax.servlet.http. HttpUtils is deprecated - so I updated the methods to // be a bit smarter // and use Map instead of Hashtable // public static Map parseQueryString(final String s) { String[] valArray = null; if (s == null) { throw new IllegalArgumentException(); } Map<String, String[]> rtn = new HashMap<String, String[]>(); StringBuffer sb = new StringBuffer(); String key; for (StringTokenizer st = new StringTokenizer(s, "&"); st.hasMoreTokens(); rtn.put(key, valArray)) { // $NON-NLS-1$ String pair = st.nextToken(); int pos = pair.indexOf('='); if (pos == -1) { throw new IllegalArgumentException(); } key = HttpUtil.parseName(pair.substring(0, pos), sb); String val = HttpUtil.parseName(pair.substring(pos + 1, pair.length()), sb); if (rtn.containsKey(key)) { String[] oldVals = rtn.get(key); valArray = new String[oldVals.length + 1]; System.arraycopy(oldVals, 0, valArray, 0, oldVals.length); valArray[oldVals.length] = val; } else { valArray = new String[1]; valArray[0] = val; } } return rtn; }
private String getUrl(final String url) { StringBuffer content = new StringBuffer(); try { // check to see if this URL failed before thia session if ((getSession() != null) && (getSession().getAttribute("pentaho-HtmlComponent-failed-url-" + url) != null)) { //$NON-NLS-1$ return errorMessage; } if (BaseUIComponent.debug) { debug(Messages.getInstance().getString("Html.DEBUG_GETTING_CONTENT", url)); // $NON-NLS-1$ } if (HttpUtil.getURLContent(url, content)) { return content.toString(); } else { getSession() .setAttribute( "pentaho-HtmlComponent-failed-url-" + url, ""); // $NON-NLS-1$ //$NON-NLS-2$ return errorMessage; } } catch (Exception e) { if (errorMessage != null) { return errorMessage; } else { error( Messages.getInstance() .getErrorString("Html.ERROR_0001_COULD_NOT_GET_CONTENT", url)); // $NON-NLS-1$ return Messages.getInstance() .getErrorString("Html.ERROR_0001_COULD_NOT_GET_CONTENT", url); // $NON-NLS-1$ } } }
public static boolean getURLContent(final String url, final StringBuffer content) throws MalformedURLException, IOException { HttpClient httpClient = HttpUtil.getClient(); try { GetMethod call = new GetMethod(url); int status = httpClient.executeMethod(call); if (status == 200) { InputStream response = call.getResponseBodyAsStream(); try { byte[] buffer = new byte[2048]; int size = response.read(buffer); while (size > 0) { for (int idx = 0; idx < size; idx++) { content.append((char) buffer[idx]); } size = response.read(buffer); } } catch (Exception e) { // we can ignore this because the content comparison will fail } } } catch (Throwable e) { StringWriter writer = new StringWriter(); PrintWriter writer2 = new PrintWriter(writer); e.printStackTrace(writer2); content.append(writer.getBuffer()); return false; } return true; }
public static String getURLContent(final String uri) { try { StringBuffer content = new StringBuffer(); HttpUtil.getURLContent(uri, content); return content.toString(); } catch (Exception e) { // TODO: handle this error Logger.error( "org.pentaho.platform.util.web.HttpUtil", Messages.getInstance().getErrorString("HttpUtil.ERROR_0001_URL_ERROR", e.getMessage()), e); //$NON-NLS-1$ //$NON-NLS-2$ return null; } }