[overlord-commits] Overlord SVN: r766 - bpm-console/trunk/gui/war/src/main/java/jmaki/xhp.
overlord-commits at lists.jboss.org
overlord-commits at lists.jboss.org
Tue Aug 18 09:59:58 EDT 2009
Author: heiko.braun at jboss.com
Date: 2009-08-18 09:59:58 -0400 (Tue, 18 Aug 2009)
New Revision: 766
Modified:
bpm-console/trunk/gui/war/src/main/java/jmaki/xhp/HttpClient.java
bpm-console/trunk/gui/war/src/main/java/jmaki/xhp/XmlHttpProxy.java
bpm-console/trunk/gui/war/src/main/java/jmaki/xhp/XmlHttpProxyServlet.java
Log:
fix proxy authentication
Modified: bpm-console/trunk/gui/war/src/main/java/jmaki/xhp/HttpClient.java
===================================================================
--- bpm-console/trunk/gui/war/src/main/java/jmaki/xhp/HttpClient.java 2009-08-18 12:07:48 UTC (rev 765)
+++ bpm-console/trunk/gui/war/src/main/java/jmaki/xhp/HttpClient.java 2009-08-18 13:59:58 UTC (rev 766)
@@ -6,12 +6,15 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
+import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.Security;
import java.util.Iterator;
import java.util.Map;
+import java.util.List;
+import java.util.HashMap;
import java.util.logging.Logger;
/**
@@ -23,246 +26,294 @@
*/
public class HttpClient {
- private static Logger logger;
- private String proxyHost = null;
- private int proxyPort = -1;
- private boolean isHttps = false;
- private boolean isProxy = false;
- private HttpURLConnection urlConnection = null;
- private Map headers;
+ private static Logger logger;
+ private String proxyHost = null;
+ private int proxyPort = -1;
+ private boolean isHttps = false;
+ private boolean isProxy = false;
+ private HttpURLConnection urlConnection = null;
+ private Map headers;
- /**
- * @param phost PROXY host name
- * @param pport PROXY port string
- * @param url URL string
- * @param headers Map
- */
- public HttpClient(
- String phost,
- int pport,
- String url,
- Map headers,
- String method)
- throws MalformedURLException
- {
- if (phost != null && pport != -1)
+ private String setCookieHeader;
+
+ private XmlHttpProxy.CookieCallback callback;
+
+ /**
+ * @param phost PROXY host name
+ * @param pport PROXY port string
+ * @param url URL string
+ * @param headers Map
+ */
+ public HttpClient(
+ String phost,
+ int pport,
+ String url,
+ Map headers,
+ String method,
+ XmlHttpProxy.CookieCallback callback)
+ throws MalformedURLException
+ {
+ this.callback = callback;
+
+ if (phost != null && pport != -1)
+ {
+ this.isProxy = true;
+ }
+
+ this.proxyHost = phost;
+ this.proxyPort = pport;
+
+ if (url.trim().startsWith("https:")) {
+ isHttps = true;
+ }
+
+ this.urlConnection = getURLConnection(url);
+ try {
+ this.urlConnection.setRequestMethod(method);
+ } catch (java.net.ProtocolException pe) {
+ HttpClient.getLogger().severe("Unable protocol method to " + method + " : " + pe);
+ }
+ this.headers = headers;
+ writeHeaders(headers);
+
+ }
+
+ private void writeHeaders(Map headers)
+ {
+ if(this.callback!=null)
+ {
+ for(XmlHttpProxy.Cookie c : callback.getCookies())
{
- this.isProxy = true;
+ if(headers==null) headers = new HashMap();
+ headers.put(
+ "Cookie", c.name + "=" + c.value // + "; Path=" + c.path
+ );
}
+ }
+ // set headers
+ if (headers != null) {
+ Iterator it = headers.keySet().iterator();
+ if (it != null) {
+ while (it.hasNext()) {
+ String key = (String)it.next();
+ String value = (String)headers.get(key);
+ System.out.println("Set Request Header: "+key + "->"+value);
+ this.urlConnection.setRequestProperty (key, value);
+ }
+ }
+ }
+ }
+
+ /**
+ * @param phost PROXY host name
+ * @param pport PROXY port string
+ * @param url URL string
+ * @param headers Map
+ * @param userName string
+ * @param password string
+ */
+ public HttpClient(String phost,
+ int pport,
+ String url,
+ Map headers,
+ String method,
+ String userName,
+ String password,
+ XmlHttpProxy.CookieCallback callback)
+ throws MalformedURLException {
+
+ this.callback = callback;
+
+ try
+ {
+ if (phost != null && pport != -1) {
+ this.isProxy = true;
+ }
+
this.proxyHost = phost;
this.proxyPort = pport;
-
if (url.trim().startsWith("https:")) {
- isHttps = true;
+ isHttps = true;
}
-
this.urlConnection = getURLConnection(url);
try {
- this.urlConnection.setRequestMethod(method);
+ this.urlConnection.setRequestMethod(method);
} catch (java.net.ProtocolException pe) {
- HttpClient.getLogger().severe("Unable protocol method to " + method + " : " + pe);
+ HttpClient.getLogger().severe("Unable protocol method to " + method + " : " + pe);
}
+ // set basic authentication information
+ String auth = userName + ":" + password;
+ String encoded = new sun.misc.BASE64Encoder().encode (auth.getBytes());
+ // set basic authorization
+ this.urlConnection.setRequestProperty ("Authorization", "Basic " + encoded);
this.headers = headers;
- // seat headers
- if (headers != null) {
- Iterator it = headers.keySet().iterator();
- if (it != null) {
- while (it.hasNext()) {
- String key = (String)it.next();
- String value = (String)headers.get(key);
- this.urlConnection.setRequestProperty (key, value);
- }
- }
+ writeHeaders(headers);
+ } catch (Exception ex) {
+ HttpClient.getLogger().severe("Unable to set basic authorization for " + userName + " : " +ex);
+ }
+ }
+
+ /**
+ * private method to get the URLConnection
+ * @param str URL string
+ */
+ private HttpURLConnection getURLConnection(String str)
+ throws MalformedURLException {
+ try {
+
+ if (isHttps) {
+ /* when communicating with the server which has unsigned or invalid
+ * certificate (https), SSLException or IOException is thrown.
+ * the following line is a hack to avoid that
+ */
+ Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
+ System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");
+ if (isProxy) {
+ System.setProperty("https.proxyHost", proxyHost);
+ System.setProperty("https.proxyPort", proxyPort + "");
+ }
}
- }
-
- /**
- * @param phost PROXY host name
- * @param pport PROXY port string
- * @param url URL string
- * @param headers Map
- * @param userName string
- * @param password string
- */
- public HttpClient(String phost,
- int pport,
- String url,
- Map headers,
- String method,
- String userName,
- String password)
- throws MalformedURLException {
- try
+ else
{
- if (phost != null && pport != -1) {
- this.isProxy = true;
- }
+ if (isProxy)
+ {
+ System.setProperty("http.proxyHost", proxyHost);
+ System.setProperty("http.proxyPort", proxyPort + "");
+ }
+ }
- this.proxyHost = phost;
- this.proxyPort = pport;
- if (url.trim().startsWith("https:")) {
- isHttps = true;
- }
- this.urlConnection = getURLConnection(url);
- try {
- this.urlConnection.setRequestMethod(method);
- } catch (java.net.ProtocolException pe) {
- HttpClient.getLogger().severe("Unable protocol method to " + method + " : " + pe);
- }
- // set basic authentication information
- String auth = userName + ":" + password;
- String encoded = new sun.misc.BASE64Encoder().encode (auth.getBytes());
- // set basic authorization
- this.urlConnection.setRequestProperty ("Authorization", "Basic " + encoded);
- this.headers = headers;
- // seat headers
- if (headers != null) {
- Iterator it = headers.entrySet().iterator();
- if (it != null) {
- while (it.hasNext()) {
- String key = (String)it.next();
- String value = (String)headers.get(key);
- this.urlConnection.setRequestProperty (key, value);
- }
- }
- }
- } catch (Exception ex) {
- HttpClient.getLogger().severe("Unable to set basic authorization for " + userName + " : " +ex);
+ URL url = new URL(str);
+ HttpURLConnection uc = (HttpURLConnection)url.openConnection();
+
+ // if this header has not been set by a request set the user agent.
+ if (headers == null ||
+ (headers != null && headers.get("user-agent") == null)) {
+ // set user agent to mimic a common browser
+ String ua="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)";
+ uc.setRequestProperty("user-agent", ua);
}
- }
- /**
- * private method to get the URLConnection
- * @param str URL string
- */
- private HttpURLConnection getURLConnection(String str)
- throws MalformedURLException {
- try {
+ uc.setInstanceFollowRedirects(false);
- if (isHttps) {
- /* when communicating with the server which has unsigned or invalid
- * certificate (https), SSLException or IOException is thrown.
- * the following line is a hack to avoid that
- */
- Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
- System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");
- if (isProxy) {
- System.setProperty("https.proxyHost", proxyHost);
- System.setProperty("https.proxyPort", proxyPort + "");
- }
- }
- else
- {
- if (isProxy)
- {
- System.setProperty("http.proxyHost", proxyHost);
- System.setProperty("http.proxyPort", proxyPort + "");
- }
- }
+ return uc;
+ }
+ catch (MalformedURLException me)
+ {
+ throw new MalformedURLException(str + " is not a valid URL");
+ }
+ catch (Exception e)
+ {
+ throw new RuntimeException("Unknown error creating UrlConnection: " + e);
+ }
+ }
- URL url = new URL(str);
- HttpURLConnection uc = (HttpURLConnection)url.openConnection();
- // if this header has not been set by a request set the user agent.
- if (headers == null ||
- (headers != null && headers.get("user-agent") == null)) {
- // set user agent to mimic a common browser
- String ua="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)";
- uc.setRequestProperty("user-agent", ua);
- }
- return uc;
- }
- catch (MalformedURLException me)
+ public String getSetCookieHeader()
+ {
+ return setCookieHeader;
+ }
+
+ /**
+ * returns the inputstream from URLConnection
+ * @return InputStream
+ */
+ public InputStream getInputStream() {
+ try
+ {
+ // logger doesnt work, because it writes to stderr,
+ // which causes GwtTest to interpret it as failure
+ System.out.println(
+ this.urlConnection.getRequestMethod()+ " " +
+ this.urlConnection.getURL() +": "+
+ this.urlConnection.getResponseCode()
+ );
+
+ try
{
- throw new MalformedURLException(str + " is not a valid URL");
+ // HACK: manually follow redirects, for the login to work
+ // HTTPUrlConnection auto redirect doesn't respect the provided headers
+ if(this.urlConnection.getResponseCode()==302)
+ {
+ HttpClient redirectClient =
+ new HttpClient(proxyHost,proxyPort, urlConnection.getHeaderField("Location"),
+ headers, urlConnection.getRequestMethod(), callback);
+ redirectClient.getInputStream().close();
+ }
}
- catch (Exception e)
+ catch (Throwable e)
{
- throw new RuntimeException("Unknown error creating UrlConnection: " + e);
+ System.out.println("Following redirect failed");
}
- }
- /**
- * returns the inputstream from URLConnection
- * @return InputStream
- */
- public InputStream getInputStream() {
- try
- {
- // logger doesnt work, because it writes to stderr,
- // which causes GwtTest to interpret it as failure
- System.out.println(
- this.urlConnection.getRequestMethod()+ " " +
- this.urlConnection.getURL() +": "+
- this.urlConnection.getResponseCode()
- );
- return (this.urlConnection.getInputStream());
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
+ setCookieHeader = this.urlConnection.getHeaderField("Set-Cookie");
- /**
- * return the OutputStream from URLConnection
- * @return OutputStream
- */
- public OutputStream getOutputStream() {
+ return (this.urlConnection.getInputStream());
+ } catch (Exception e) {
+ e.printStackTrace();
+ return null;
+ }
+ }
- try {
- return (this.urlConnection.getOutputStream());
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
+ /**
+ * return the OutputStream from URLConnection
+ * @return OutputStream
+ */
+ public OutputStream getOutputStream() {
- /**
- * posts data to the inputstream and returns the InputStream.
- * @param postData data to be posted. must be url-encoded already.
- * @param contentType allows you to set the contentType of the request.
- * @return InputStream input stream from URLConnection
- */
- public InputStream doPost(String postData, String contentType) {
- this.urlConnection.setDoOutput(true);
- if (contentType != null) this.urlConnection.setRequestProperty( "Content-type", contentType );
+ try {
+ return (this.urlConnection.getOutputStream());
+ } catch (Exception e) {
+ e.printStackTrace();
+ return null;
+ }
+ }
- OutputStream os = this.getOutputStream();
- PrintStream ps = new PrintStream(os);
- ps.print(postData);
- ps.close();
- return (this.getInputStream());
- }
+ /**
+ * posts data to the inputstream and returns the InputStream.
+ * @param postData data to be posted. must be url-encoded already.
+ * @param contentType allows you to set the contentType of the request.
+ * @return InputStream input stream from URLConnection
+ */
+ public InputStream doPost(String postData, String contentType) {
+ this.urlConnection.setDoOutput(true);
+ if (contentType != null) this.urlConnection.setRequestProperty( "Content-type", contentType );
- public String getContentEncoding() {
- if (this.urlConnection == null) return null;
- return (this.urlConnection.getContentEncoding());
- }
- public int getContentLength() {
- if (this.urlConnection == null) return -1;
- return (this.urlConnection.getContentLength());
- }
- public String getContentType() {
- if (this.urlConnection == null) return null;
- return (this.urlConnection.getContentType());
- }
- public long getDate() {
- if (this.urlConnection == null) return -1;
- return (this.urlConnection.getDate());
- }
- public String getHeader(String name) {
- if (this.urlConnection == null) return null;
- return (this.urlConnection.getHeaderField(name));
- }
- public long getIfModifiedSince() {
- if (this.urlConnection == null) return -1;
- return (this.urlConnection.getIfModifiedSince());
- }
+ OutputStream os = this.getOutputStream();
+ PrintStream ps = new PrintStream(os);
+ ps.print(postData);
+ ps.close();
+ return (this.getInputStream());
+ }
- public static Logger getLogger() {
- if (logger == null) {
- logger = Logger.getLogger("jmaki.xhp.Log");
- }
- return logger;
- }
+ public String getContentEncoding() {
+ if (this.urlConnection == null) return null;
+ return (this.urlConnection.getContentEncoding());
+ }
+ public int getContentLength() {
+ if (this.urlConnection == null) return -1;
+ return (this.urlConnection.getContentLength());
+ }
+ public String getContentType() {
+ if (this.urlConnection == null) return null;
+ return (this.urlConnection.getContentType());
+ }
+ public long getDate() {
+ if (this.urlConnection == null) return -1;
+ return (this.urlConnection.getDate());
+ }
+ public String getHeader(String name) {
+ if (this.urlConnection == null) return null;
+ return (this.urlConnection.getHeaderField(name));
+ }
+ public long getIfModifiedSince() {
+ if (this.urlConnection == null) return -1;
+ return (this.urlConnection.getIfModifiedSince());
+ }
+
+ public static Logger getLogger() {
+ if (logger == null) {
+ logger = Logger.getLogger("jmaki.xhp.Log");
+ }
+ return logger;
+ }
}
\ No newline at end of file
Modified: bpm-console/trunk/gui/war/src/main/java/jmaki/xhp/XmlHttpProxy.java
===================================================================
--- bpm-console/trunk/gui/war/src/main/java/jmaki/xhp/XmlHttpProxy.java 2009-08-18 12:07:48 UTC (rev 765)
+++ bpm-console/trunk/gui/war/src/main/java/jmaki/xhp/XmlHttpProxy.java 2009-08-18 13:59:58 UTC (rev 766)
@@ -19,382 +19,445 @@
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.Map;
+import java.util.*;
import java.util.logging.Logger;
public class XmlHttpProxy {
- public static String GET = "GET";
- public static String POST = "POST";
- public static String DELETE = "DELETE";
- public static String PUT = "PUT";
+ public static String GET = "GET";
+ public static String POST = "POST";
+ public static String DELETE = "DELETE";
+ public static String PUT = "PUT";
- private String userName = null;
- private String password = null;
- private static Logger logger;
- private String proxyHost = "";
- int proxyPort = -1;
- private JSONObject config;
- private static String USAGE = "Usage: -url service_URL -id service_key [-url or -id required] -xslurl xsl_url [optional] -format json|xml [optional] -callback[optional] -config [optional] -resources base_directory_containing XSL stylesheets [optional]";
+ private String userName = null;
+ private String password = null;
+ private static Logger logger;
+ private String proxyHost = "";
+ int proxyPort = -1;
+ private JSONObject config;
+ private static String USAGE = "Usage: -url service_URL -id service_key [-url or -id required] -xslurl xsl_url [optional] -format json|xml [optional] -callback[optional] -config [optional] -resources base_directory_containing XSL stylesheets [optional]";
- public XmlHttpProxy() {}
+ public XmlHttpProxy() {}
- public XmlHttpProxy(String proxyHost, int proxyPort) {
- this.proxyHost = proxyHost;
- this.proxyPort = proxyPort;
- }
+ private Set<Cookie> cookies = new HashSet<Cookie>();
- public XmlHttpProxy(String proxyHost, int proxyPort,
- String userName, String password) {
- this.proxyHost = proxyHost;
- this.proxyPort = proxyPort;
- this.userName = userName;
- this.password = password;
- }
+ public interface CookieCallback
+ {
+ Set<Cookie> getCookies();
+ }
- /**
- * This method will go out and make the call and it will apply an XSLT Transformation with the
- * set of parameters provided.
- *
- * @param urlString - The URL which you are looking up
- * @param out - The OutputStream to which the resulting document is written
- * @param xslInputStream - An input Stream to an XSL style sheet that is provided to the XSLT processor. If set to null there will be no transformation
- * @param paramsMap - A Map of parameters that are feed to the XSLT Processor. These params may be used when generating content. This may be set to null if no parameters are necessary.
- * @param method - The HTTP method used.
- *
- */
- public void processRequest(String urlString,
- OutputStream out,
- InputStream xslInputStream,
- Map paramsMap,
- Map headers,
- String method,
- String userName,
- String password) throws IOException, MalformedURLException {
- doProcess(urlString, out, xslInputStream, paramsMap, headers,method, null,null, userName,password);
- }
- /**
- * This method will go out and make the call and it will apply an XSLT Transformation with the
- * set of parameters provided.
- *
- * @param urlString - The URL which you are looking up
- * @param out - The OutputStream to which the resulting document is written
- *
- */
- public void doPost(String urlString,
- OutputStream out,
- InputStream xslInputStream,
- Map paramsMap,
- Map headers,
- String postData,
- String postContentType,
- String userName,
- String password) throws IOException, MalformedURLException {
- doProcess(urlString, out, xslInputStream, paramsMap, headers, XmlHttpProxy.POST, postData, postContentType, userName, password);
- }
+ public XmlHttpProxy(String proxyHost, int proxyPort) {
+ this.proxyHost = proxyHost;
+ this.proxyPort = proxyPort;
+ }
- /**
- * This method will go out and make the call and it will apply an XSLT Transformation with the
- * set of parameters provided.
- *
- * @param urlString - The URL which you are looking up
- * @param out - The OutputStream to which the resulting document is written
- * @param xslInputStream - An input Stream to an XSL style sheet that is provided to the XSLT processor. If set to null there will be no transformation
- * @param paramsMap - A Map of parameters that are feed to the XSLT Processor. These params may be used when generating content. This may be set to null if no parameters are necessary.
- * @param method - the HTTP method used.
- * @param postData - A String of the bodyContent to be posted. A doPost will be used if this is parameter is not null.
- * @param postContentType - The request contentType used when posting data. Will not be set if this parameter is null.
- * @param userName - userName used for basic authorization
- * @param password - password used for basic authorization
- */
- public void doProcess(String urlString,
- OutputStream out,
- InputStream xslInputStream,
- Map paramsMap,
- Map headers,
- String method,
- String postData,
- String postContentType,
- String userName,
- String password) throws IOException, MalformedURLException {
-
- if (paramsMap == null) {
- paramsMap = new HashMap();
- }
+ public XmlHttpProxy(String proxyHost, int proxyPort,
+ String userName, String password) {
+ this.proxyHost = proxyHost;
+ this.proxyPort = proxyPort;
+ this.userName = userName;
+ this.password = password;
+ }
- String format = (String)paramsMap.get("format");
- if (format == null) {
- format = "xml";
- }
+ /**
+ * This method will go out and make the call and it will apply an XSLT Transformation with the
+ * set of parameters provided.
+ *
+ * @param urlString - The URL which you are looking up
+ * @param out - The OutputStream to which the resulting document is written
+ * @param xslInputStream - An input Stream to an XSL style sheet that is provided to the XSLT processor. If set to null there will be no transformation
+ * @param paramsMap - A Map of parameters that are feed to the XSLT Processor. These params may be used when generating content. This may be set to null if no parameters are necessary.
+ * @param method - The HTTP method used.
+ *
+ */
+ public void processRequest(String urlString,
+ OutputStream out,
+ InputStream xslInputStream,
+ Map paramsMap,
+ Map headers,
+ String method,
+ String userName,
+ String password) throws IOException, MalformedURLException {
+ doProcess(urlString, out, xslInputStream, paramsMap, headers,method, null,null, userName,password);
+ }
+ /**
+ * This method will go out and make the call and it will apply an XSLT Transformation with the
+ * set of parameters provided.
+ *
+ * @param urlString - The URL which you are looking up
+ * @param out - The OutputStream to which the resulting document is written
+ *
+ */
+ public void doPost(String urlString,
+ OutputStream out,
+ InputStream xslInputStream,
+ Map paramsMap,
+ Map headers,
+ String postData,
+ String postContentType,
+ String userName,
+ String password) throws IOException, MalformedURLException {
+ doProcess(urlString, out, xslInputStream, paramsMap, headers, XmlHttpProxy.POST, postData, postContentType, userName, password);
+ }
- InputStream in = null;
- BufferedOutputStream os = null;
+ /**
+ * This method will go out and make the call and it will apply an XSLT Transformation with the
+ * set of parameters provided.
+ *
+ * @param urlString - The URL which you are looking up
+ * @param out - The OutputStream to which the resulting document is written
+ * @param xslInputStream - An input Stream to an XSL style sheet that is provided to the XSLT processor. If set to null there will be no transformation
+ * @param paramsMap - A Map of parameters that are feed to the XSLT Processor. These params may be used when generating content. This may be set to null if no parameters are necessary.
+ * @param method - the HTTP method used.
+ * @param postData - A String of the bodyContent to be posted. A doPost will be used if this is parameter is not null.
+ * @param postContentType - The request contentType used when posting data. Will not be set if this parameter is null.
+ * @param userName - userName used for basic authorization
+ * @param password - password used for basic authorization
+ */
+ public void doProcess(String urlString,
+ OutputStream out,
+ InputStream xslInputStream,
+ Map paramsMap,
+ Map headers,
+ String method,
+ String postData,
+ String postContentType,
+ String userName,
+ String password) throws IOException, MalformedURLException {
- HttpClient httpclient = null;
+ if (paramsMap == null) {
+ paramsMap = new HashMap();
+ }
- if (userName != null && password != null)
+ String format = (String)paramsMap.get("format");
+ if (format == null) {
+ format = "xml";
+ }
+
+ InputStream in = null;
+ BufferedOutputStream os = null;
+
+ HttpClient httpclient = null;
+
+ CookieCallback callback = new CookieCallback()
+ {
+
+ public Set<Cookie> getCookies()
{
- httpclient = new HttpClient(proxyHost, proxyPort, urlString, headers, method, userName, password);
+ return accessCookies();
}
- else
+ };
+
+ if (userName != null && password != null)
+ {
+ httpclient = new HttpClient(proxyHost, proxyPort, urlString, headers, method, userName, password, callback);
+ }
+ else
+ {
+ httpclient = new HttpClient(proxyHost, proxyPort, urlString, headers, method, callback);
+ }
+
+ // post data determines whether we are going to do a get or a post
+ if (postData == null) {
+ in = httpclient.getInputStream();
+ } else {
+ in = httpclient.doPost(postData, postContentType);
+ }
+
+ // Set-Cookie header
+ if(httpclient.getSetCookieHeader()!=null)
+ {
+ String cookie = httpclient.getSetCookieHeader();
+ System.out.println("'Set-Cookie' header: "+ cookie);
+ String[] values = cookie.split(";");
+
+ Cookie c = new Cookie();
+ for(String v : values)
{
- httpclient = new HttpClient(proxyHost, proxyPort, urlString, headers, method);
+ String[] tuple = v.split("=");
+ if("Path".equals( tuple[0].trim()))
+ c.path = tuple[1];
+ else
+ {
+ c.name = tuple[0].trim();
+ c.value = tuple[1];
+ }
}
- // post data determines whether we are going to do a get or a post
- if (postData == null) {
- in = httpclient.getInputStream();
- } else {
- in = httpclient.doPost(postData, postContentType);
- }
+ for(Cookie exists : cookies)
+ {
+ if(exists.name.equals(c.name))
+ {
+ String msg = exists.value.equals(c.value) ?
+ "Replace with same value: "+exists.value :
+ "Replace with different value: "+exists.value +"->"+c.value;
- if(null==in)
- {
- throw new IOException("Failed to open input stream");
+ System.out.println("Cookie '"+exists.name+"' exists: " + msg);
+ // avoid doubles
+ cookies.remove(exists);
+ }
}
+
+ cookies.add(c);
+ }
+
+ if(null==in)
+ {
+ throw new IOException("Failed to open input stream");
+ }
- // read the encoding from the incoming document and default to UTF-8
- // if an encoding is not provided
- String ce = httpclient.getContentEncoding();
- if (ce == null) {
- String ct = httpclient.getContentType();
- if (ct != null) {
- int idx = ct.lastIndexOf("charset=");
- if (idx >= 0) {
- ce = ct.substring(idx+8);
- } else {
- ce = "UTF-8";
- }
- } else {
- ce = "UTF-8";
- }
+ // read the encoding from the incoming document and default to UTF-8
+ // if an encoding is not provided
+ String ce = httpclient.getContentEncoding();
+ if (ce == null) {
+ String ct = httpclient.getContentType();
+ if (ct != null) {
+ int idx = ct.lastIndexOf("charset=");
+ if (idx >= 0) {
+ ce = ct.substring(idx+8);
+ } else {
+ ce = "UTF-8";
+ }
+ } else {
+ ce = "UTF-8";
}
- // get the content type
- String cType = null;
- // write out the content type
- //http://www.ietf.org/rfc/rfc4627.txt
- if (format.equals("json")) {
- cType = "application/json;charset="+ce;
+ }
+ // get the content type
+ String cType = null;
+ // write out the content type
+ //http://www.ietf.org/rfc/rfc4627.txt
+ if (format.equals("json")) {
+ cType = "application/json;charset="+ce;
+ } else {
+ cType = "text/xml;charset="+ce;
+ }
+ try {
+ byte[] buffer = new byte[1024];
+ int read = 0;
+ if (xslInputStream == null) {
+ while (true) {
+ read = in.read(buffer);
+ if (read <= 0) break;
+ out.write(buffer, 0, read );
+ }
} else {
- cType = "text/xml;charset="+ce;
+ transform(in, xslInputStream, paramsMap, out, ce);
}
+ } catch (Exception e) {
+ getLogger().severe("XmlHttpProxy transformation error: " + e);
+ } finally {
try {
- byte[] buffer = new byte[1024];
- int read = 0;
- if (xslInputStream == null) {
- while (true) {
- read = in.read(buffer);
- if (read <= 0) break;
- out.write(buffer, 0, read );
- }
- } else {
- transform(in, xslInputStream, paramsMap, out, ce);
- }
+ if (in != null) {
+ in.close();
+ }
+ if (out != null) {
+ out.flush();
+ out.close();
+ }
} catch (Exception e) {
- getLogger().severe("XmlHttpProxy transformation error: " + e);
- } finally {
- try {
- if (in != null) {
- in.close();
- }
- if (out != null) {
- out.flush();
- out.close();
- }
- } catch (Exception e) {
- // do nothing
- }
+ // do nothing
}
- }
+ }
+ }
- /**
- * Do the XSLT transformation
- */
- public void transform( InputStream xmlIS,
- InputStream xslIS,
- Map params,
- OutputStream result,
- String encoding) {
- try {
- TransformerFactory trFac = TransformerFactory.newInstance();
- Transformer transformer = trFac.newTransformer(new StreamSource(xslIS));
- Iterator it = params.keySet().iterator();
- while (it.hasNext()) {
- String key = (String)it.next();
- transformer.setParameter(key, (String)params.get(key));
- }
- transformer.setOutputProperty("encoding", encoding);
- transformer.transform(new StreamSource(xmlIS), new StreamResult(result));
- } catch (Exception e) {
- getLogger().severe("XmlHttpProxy: Exception with xslt " + e);
+ private Set<Cookie> accessCookies()
+ {
+ return cookies;
+ }
+
+ /**
+ * Do the XSLT transformation
+ */
+ public void transform( InputStream xmlIS,
+ InputStream xslIS,
+ Map params,
+ OutputStream result,
+ String encoding) {
+ try {
+ TransformerFactory trFac = TransformerFactory.newInstance();
+ Transformer transformer = trFac.newTransformer(new StreamSource(xslIS));
+ Iterator it = params.keySet().iterator();
+ while (it.hasNext()) {
+ String key = (String)it.next();
+ transformer.setParameter(key, (String)params.get(key));
}
- }
+ transformer.setOutputProperty("encoding", encoding);
+ transformer.transform(new StreamSource(xmlIS), new StreamResult(result));
+ } catch (Exception e) {
+ getLogger().severe("XmlHttpProxy: Exception with xslt " + e);
+ }
+ }
- /**
- *
- * CLI to the XmlHttpProxy
- */
- public static void main(String[] args)
- throws IOException, MalformedURLException {
+ /**
+ *
+ * CLI to the XmlHttpProxy
+ */
+ public static void main(String[] args)
+ throws IOException, MalformedURLException {
- getLogger().info("XmlHttpProxy 1.8");
- XmlHttpProxy xhp = new XmlHttpProxy();
+ getLogger().info("XmlHttpProxy 1.8");
+ XmlHttpProxy xhp = new XmlHttpProxy();
- if (args.length == 0) {
- System.out.println(USAGE);
- }
+ if (args.length == 0) {
+ System.out.println(USAGE);
+ }
- String method = XmlHttpProxy.GET;
- InputStream xslInputStream = null;
- String serviceKey = null;
- String urlString = null;
- String xslURLString = null;
- String format = "xml";
- String callback = null;
- String urlParams = null;
- String configURLString = "xhp.json";
- String resourceBase = "file:src/conf/META-INF/resources/xsl/";
- String username = null;
- String password = null;
+ String method = XmlHttpProxy.GET;
+ InputStream xslInputStream = null;
+ String serviceKey = null;
+ String urlString = null;
+ String xslURLString = null;
+ String format = "xml";
+ String callback = null;
+ String urlParams = null;
+ String configURLString = "xhp.json";
+ String resourceBase = "file:src/conf/META-INF/resources/xsl/";
+ String username = null;
+ String password = null;
- // read in the arguments
- int index = 0;
- while (index < args.length) {
- if (args[index].toLowerCase().equals("-url") && index + 1 < args.length) {
- urlString = args[++index];
- } else if (args[index].toLowerCase().equals("-key") && index + 1 < args.length) {
- serviceKey = args[++index];
- } else if (args[index].toLowerCase().equals("-id") && index + 1 < args.length) {
- serviceKey = args[++index];
- } else if (args[index].toLowerCase().equals("-callback") && index + 1 < args.length) {
- callback = args[++index];
- } else if (args[index].toLowerCase().equals("-xslurl") && index + 1 < args.length) {
- xslURLString = args[++index];
- } else if (args[index].toLowerCase().equals("-method") && index + 1 < args.length) {
- method = args[++index];
- } else if (args[index].toLowerCase().equals("-username") && index + 1 < args.length) {
- username = args[++index];
- } else if (args[index].toLowerCase().equals("-password") && index + 1 < args.length) {
- password = args[++index];
- } else if (args[index].toLowerCase().equals("-urlparams") && index + 1 < args.length) {
- urlParams = args[++index];
- } else if (args[index].toLowerCase().equals("-config") && index + 1 < args.length) {
- configURLString = args[++index];
- } else if (args[index].toLowerCase().equals("-resources") && index + 1 < args.length) {
- resourceBase = args[++index];
- }
- index++;
+ // read in the arguments
+ int index = 0;
+ while (index < args.length) {
+ if (args[index].toLowerCase().equals("-url") && index + 1 < args.length) {
+ urlString = args[++index];
+ } else if (args[index].toLowerCase().equals("-key") && index + 1 < args.length) {
+ serviceKey = args[++index];
+ } else if (args[index].toLowerCase().equals("-id") && index + 1 < args.length) {
+ serviceKey = args[++index];
+ } else if (args[index].toLowerCase().equals("-callback") && index + 1 < args.length) {
+ callback = args[++index];
+ } else if (args[index].toLowerCase().equals("-xslurl") && index + 1 < args.length) {
+ xslURLString = args[++index];
+ } else if (args[index].toLowerCase().equals("-method") && index + 1 < args.length) {
+ method = args[++index];
+ } else if (args[index].toLowerCase().equals("-username") && index + 1 < args.length) {
+ username = args[++index];
+ } else if (args[index].toLowerCase().equals("-password") && index + 1 < args.length) {
+ password = args[++index];
+ } else if (args[index].toLowerCase().equals("-urlparams") && index + 1 < args.length) {
+ urlParams = args[++index];
+ } else if (args[index].toLowerCase().equals("-config") && index + 1 < args.length) {
+ configURLString = args[++index];
+ } else if (args[index].toLowerCase().equals("-resources") && index + 1 < args.length) {
+ resourceBase = args[++index];
}
+ index++;
+ }
- if (serviceKey != null) {
- try {
- InputStream is = (new URL(configURLString)).openStream();
- JSONObject services = loadServices(is);
- JSONObject service = services.getJSONObject(serviceKey);
- // default to the service default if no url parameters are specified
- if (urlParams == null && service.has("defaultURLParams")) {
- urlParams = service.getString("defaultURLParams");
- }
- String serviceURL = service.getString("url");
- // build the URL properly
- if (urlParams != null && serviceURL.indexOf("?") == -1){
- serviceURL += "?";
- } else if (urlParams != null){
- serviceURL += "&";
- }
- String apiKey = "";
- if (service.has("apikey")) apiKey = service.getString("apikey");
- urlString = serviceURL + apiKey + "&" + urlParams;
- if (service.has("xslStyleSheet")) {
- xslURLString = service.getString("xslStyleSheet");
- // check if the url is correct of if to load from the classpath
+ if (serviceKey != null) {
+ try {
+ InputStream is = (new URL(configURLString)).openStream();
+ JSONObject services = loadServices(is);
+ JSONObject service = services.getJSONObject(serviceKey);
+ // default to the service default if no url parameters are specified
+ if (urlParams == null && service.has("defaultURLParams")) {
+ urlParams = service.getString("defaultURLParams");
+ }
+ String serviceURL = service.getString("url");
+ // build the URL properly
+ if (urlParams != null && serviceURL.indexOf("?") == -1){
+ serviceURL += "?";
+ } else if (urlParams != null){
+ serviceURL += "&";
+ }
+ String apiKey = "";
+ if (service.has("apikey")) apiKey = service.getString("apikey");
+ urlString = serviceURL + apiKey + "&" + urlParams;
+ if (service.has("xslStyleSheet")) {
+ xslURLString = service.getString("xslStyleSheet");
+ // check if the url is correct of if to load from the classpath
- }
- } catch (Exception ex) {
- getLogger().severe("XmlHttpProxy Error loading service: " + ex);
- System.exit(1);
- }
- } else if (urlString == null) {
- System.out.println(USAGE);
- System.exit(1);
+ }
+ } catch (Exception ex) {
+ getLogger().severe("XmlHttpProxy Error loading service: " + ex);
+ System.exit(1);
}
- // The parameters are feed to the XSL Stylsheet during transformation.
- // These parameters can provided data or conditional information.
- Map paramsMap = new HashMap();
- if (format != null) {
- paramsMap.put("format", format);
- }
- if (callback != null) {
- paramsMap.put("callback", callback);
- }
+ } else if (urlString == null) {
+ System.out.println(USAGE);
+ System.exit(1);
+ }
+ // The parameters are feed to the XSL Stylsheet during transformation.
+ // These parameters can provided data or conditional information.
+ Map paramsMap = new HashMap();
+ if (format != null) {
+ paramsMap.put("format", format);
+ }
+ if (callback != null) {
+ paramsMap.put("callback", callback);
+ }
- if (xslURLString != null) {
- URL xslURL = new URL(xslURLString);
- if (xslURL != null) {
- xslInputStream = xslURL.openStream();
- } else {
- getLogger().severe("Error: Unable to locate XSL at URL " + xslURLString);
- }
+ if (xslURLString != null) {
+ URL xslURL = new URL(xslURLString);
+ if (xslURL != null) {
+ xslInputStream = xslURL.openStream();
+ } else {
+ getLogger().severe("Error: Unable to locate XSL at URL " + xslURLString);
}
- xhp.processRequest(urlString, System.out, xslInputStream, paramsMap, null, method, username, password);
- }
+ }
+ xhp.processRequest(urlString, System.out, xslInputStream, paramsMap, null, method, username, password);
+ }
- public static Logger getLogger() {
- if (logger == null) {
- logger = Logger.getLogger("jmaki.xhp.Log");
+ public static Logger getLogger() {
+ if (logger == null) {
+ logger = Logger.getLogger("jmaki.xhp.Log");
+ }
+ return logger;
+ }
+
+ public static JSONObject loadServices(InputStream is)
+ {
+ JSONObject config = null;
+ JSONObject services = new JSONObject();
+ try
+ {
+ config = loadJSONObject(is).getJSONObject("xhp");
+ JSONArray sA = config.getJSONArray("services");
+ for (int l=0; l < sA.length(); l++) {
+ JSONObject value = sA.getJSONObject(l);
+ String key = value.getString("id");
+ services.put(key,value);
}
- return logger;
- }
+ }
+ catch (Exception ex)
+ {
+ getLogger().severe("XmlHttpProxy error loading services." + ex);
+ }
+ return services;
+ }
- public static JSONObject loadServices(InputStream is)
- {
- JSONObject config = null;
- JSONObject services = new JSONObject();
- try
- {
- config = loadJSONObject(is).getJSONObject("xhp");
- JSONArray sA = config.getJSONArray("services");
- for (int l=0; l < sA.length(); l++) {
- JSONObject value = sA.getJSONObject(l);
- String key = value.getString("id");
- services.put(key,value);
- }
+ public static JSONObject loadJSONObject(InputStream in) {
+ ByteArrayOutputStream out = null;
+ try {
+ byte[] buffer = new byte[1024];
+ int read = 0;
+ out = new ByteArrayOutputStream();
+ while (true) {
+ read = in.read(buffer);
+ if (read <= 0) break;
+ out.write(buffer, 0, read );
}
- catch (Exception ex)
- {
- getLogger().severe("XmlHttpProxy error loading services." + ex);
- }
- return services;
- }
-
- public static JSONObject loadJSONObject(InputStream in) {
- ByteArrayOutputStream out = null;
+ return new JSONObject(out.toString());
+ } catch (Exception e) {
+ getLogger().severe("XmlHttpProxy error reading in json " + e);
+ } finally {
try {
- byte[] buffer = new byte[1024];
- int read = 0;
- out = new ByteArrayOutputStream();
- while (true) {
- read = in.read(buffer);
- if (read <= 0) break;
- out.write(buffer, 0, read );
- }
- return new JSONObject(out.toString());
+ if (in != null) {
+ in.close();
+ }
+ if (out != null) {
+ out.flush();
+ out.close();
+ }
} catch (Exception e) {
- getLogger().severe("XmlHttpProxy error reading in json " + e);
- } finally {
- try {
- if (in != null) {
- in.close();
- }
- if (out != null) {
- out.flush();
- out.close();
- }
- } catch (Exception e) {
- }
}
- return null;
- }
+ }
+ return null;
+ }
+
+ public class Cookie
+ {
+ String name;
+ String value;
+ String path;
+ }
}
\ No newline at end of file
Modified: bpm-console/trunk/gui/war/src/main/java/jmaki/xhp/XmlHttpProxyServlet.java
===================================================================
--- bpm-console/trunk/gui/war/src/main/java/jmaki/xhp/XmlHttpProxyServlet.java 2009-08-18 12:07:48 UTC (rev 765)
+++ bpm-console/trunk/gui/war/src/main/java/jmaki/xhp/XmlHttpProxyServlet.java 2009-08-18 13:59:58 UTC (rev 766)
@@ -55,6 +55,8 @@
private static String testUser;
private static String testPass;
+ private static String setCookie;
+
public XmlHttpProxyServlet() {
if (rDebug) {
logger = getLogger();
More information about the overlord-commits
mailing list