1.1 Installation
1.2 Google API Library
- 1.3 TagLib Directive + 1.3 TagLib Directive
+ 1.4 HTTP Proxy
2. Search Tags @@ -206,6 +207,31 @@ code {font-size:10pt}
+<%@taglib uri="/google-taglib" prefix="google"%>
1.4 HTTP Proxy | +Back + to top | +
+ An HTTP Proxy server can be specified using context parameters in your
+ web application deployment descriptor (web.xml
) as follows:
+
++<context-param>+
<param-name>google_proxy_host</param-name>
<param-value>proxy.example.com</param-value>
</context-param><context-param>+
<param-name>google_proxy_port</param-name>
<param-value>3128</param-value>
</context-param>
+ Similarly, the HTTP Proxy user name and password (if any) may be specified using: +
++<context-param>+
<param-name>google_proxy_username</param-name>
<param-value>john</param-value>
</context-param><context-param>+
<param-name>google_proxy_password</param-name>
<param-value>opensesame</param-value>
</context-param>
+ Please note that the Google Web APIs automatically check the standard http.proxyHost
+ and http.proxyPort
Java system properties.
diff --git a/google-taglib/src/net/thauvin/google/GoogleSearchBean.java b/google-taglib/src/net/thauvin/google/GoogleSearchBean.java index 447f08a..a51fd59 100644 --- a/google-taglib/src/net/thauvin/google/GoogleSearchBean.java +++ b/google-taglib/src/net/thauvin/google/GoogleSearchBean.java @@ -138,7 +138,7 @@ public class GoogleSearchBean { service.setKey(key); - if ((key != null) && (key.trim().length() > 0)) + if (isValidString(key)) { keySet = true; } @@ -158,6 +158,34 @@ public class GoogleSearchBean return keySet; } + /** + * Sets the HTTP proxy host, port, user name and password. + * + * @param proxyHost The host to use for the HTTP proxy. + * @param proxyPort The port to use for the HTTP proxy. + * @param proxyUserName The user name to use for the HTTP proxy. + * @param proxyPassword The password to use for the HTTP proxy. + */ + public void setProxyServer(String proxyHost, String proxyPort, + String proxyUserName, String proxyPassword) + { + int port = -1; + + if (isValidString(proxyPort)) + { + try + { + port = Integer.valueOf(proxyPort).intValue(); + } + catch (NumberFormatException e) + { + ; // Do nothing. + } + } + + setProxyServer(proxyHost, port, proxyUserName, proxyPassword); + } + /** * Sets the HTTP proxy host, port, user name and password. * @@ -169,10 +197,25 @@ public class GoogleSearchBean public void setProxyServer(String proxyHost, int proxyPort, String proxyUserName, String proxyPassword) { - service.setProxyHost(proxyHost); - service.setProxyPort(proxyPort); - service.setProxyUserName(proxyUserName); - service.setProxyPassword(proxyPassword); + if (isValidString(proxyHost)) + { + service.setProxyHost(proxyHost); + + if (proxyPort > 0) + { + service.setProxyPort(proxyPort); + } + + if (isValidString(proxyUserName)) + { + service.setProxyUserName(proxyUserName); + } + + if (isValidString(proxyPassword)) + { + service.setProxyPassword(proxyPassword); + } + } } /** @@ -641,6 +684,22 @@ public class GoogleSearchBean elements = null; } + /** + * Validates a string value by insuring it is not null or empty. + * + * @param stringValue The String value. + * @return true if valid, false if not. + */ + private boolean isValidString(String stringValue) + { + if ((stringValue != null) && (stringValue.trim().length() > 0)) + { + return true; + } + + return false; + } + /** * Prints the usage and exits. */ diff --git a/google-taglib/src/net/thauvin/google/TagUtility.java b/google-taglib/src/net/thauvin/google/TagUtility.java index bb6a87a..611e78f 100644 --- a/google-taglib/src/net/thauvin/google/TagUtility.java +++ b/google-taglib/src/net/thauvin/google/TagUtility.java @@ -67,6 +67,26 @@ public class TagUtility */ public static final String FILTER_PARAM = "filter"; + /** + * The HTTP proxy host. + */ + public static final String GOOGLE_PROXY_HOST = "google_proxy_host"; + + /** + * The HTTP proxy password. + */ + public static final String GOOGLE_PROXY_PASSWORD = "google_proxy_password"; + + /** + * The HTTP proxy port. + */ + public static final String GOOGLE_PROXY_PORT = "google_proxy_port"; + + /** + * The HTTP proxy username. + */ + public static final String GOOGLE_PROXY_USERNAME = "google_proxy_username"; + /** * The name of the Google Search bean attribute. */ diff --git a/google-taglib/src/net/thauvin/google/taglibs/CachedPage.java b/google-taglib/src/net/thauvin/google/taglibs/CachedPage.java index 831c71b..5f95617 100644 --- a/google-taglib/src/net/thauvin/google/taglibs/CachedPage.java +++ b/google-taglib/src/net/thauvin/google/taglibs/CachedPage.java @@ -4,14 +4,14 @@ * Copyright (c) 2002-2003, Erik C. Thauvin (erik@thauvin.net) * All rights reserved. * - * Redistribution and use in source and binary forms, with or without + * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * - * Redistributions in binary form must reproduce the above copyright notice, + * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * @@ -20,7 +20,7 @@ * specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS - * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, @@ -69,6 +69,15 @@ public class CachedPage extends QuerySupport { final GoogleSearchBean bean = new GoogleSearchBean(getKey()); + bean.setProxyServer(pageContext.getServletContext() + .getInitParameter(TagUtility.GOOGLE_PROXY_HOST), + pageContext.getServletContext() + .getInitParameter(TagUtility.GOOGLE_PROXY_PORT), + pageContext.getServletContext() + .getInitParameter(TagUtility.GOOGLE_PROXY_USERNAME), + pageContext.getServletContext() + .getInitParameter(TagUtility.GOOGLE_PROXY_PASSWORD)); + // Output the body pageContext.getOut().write(bean.getCachedPage(query)); } diff --git a/google-taglib/src/net/thauvin/google/taglibs/Search.java b/google-taglib/src/net/thauvin/google/taglibs/Search.java index ddf1e7f..77c8210 100644 --- a/google-taglib/src/net/thauvin/google/taglibs/Search.java +++ b/google-taglib/src/net/thauvin/google/taglibs/Search.java @@ -275,6 +275,15 @@ public class Search extends QuerySupport { try { + bean.setProxyServer(pageContext.getServletContext() + .getInitParameter(TagUtility.GOOGLE_PROXY_HOST), + pageContext.getServletContext() + .getInitParameter(TagUtility.GOOGLE_PROXY_PORT), + pageContext.getServletContext() + .getInitParameter(TagUtility.GOOGLE_PROXY_USERNAME), + pageContext.getServletContext() + .getInitParameter(TagUtility.GOOGLE_PROXY_PASSWORD)); + bean.getGoogleSearch(getKey(), getSite() + getQuery(), getStart(), getMaxResults(), getFilter(), getRestrict(), getSafeSearch(), getLr()); diff --git a/google-taglib/src/net/thauvin/google/taglibs/Spelling.java b/google-taglib/src/net/thauvin/google/taglibs/Spelling.java index 63f06d8..7814207 100644 --- a/google-taglib/src/net/thauvin/google/taglibs/Spelling.java +++ b/google-taglib/src/net/thauvin/google/taglibs/Spelling.java @@ -4,14 +4,14 @@ * Copyright (c) 2002-2003, Erik C. Thauvin (erik@thauvin.net) * All rights reserved. * - * Redistribution and use in source and binary forms, with or without + * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * - * Redistributions in binary form must reproduce the above copyright notice, + * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * @@ -20,7 +20,7 @@ * specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS - * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, @@ -69,6 +69,15 @@ public class Spelling extends QuerySupport { final GoogleSearchBean bean = new GoogleSearchBean(getKey()); + bean.setProxyServer(pageContext.getServletContext() + .getInitParameter(TagUtility.GOOGLE_PROXY_HOST), + pageContext.getServletContext() + .getInitParameter(TagUtility.GOOGLE_PROXY_PORT), + pageContext.getServletContext() + .getInitParameter(TagUtility.GOOGLE_PROXY_USERNAME), + pageContext.getServletContext() + .getInitParameter(TagUtility.GOOGLE_PROXY_PASSWORD)); + String result = bean.getSpellingSuggestion(query); if (!TagUtility.isValidString(result, true))