On Tue, 2012-11-20 at 02:42 +0000, [email protected] wrote:
> Author: ggregory
> Date: Tue Nov 20 02:42:15 2012
> New Revision: 1411518
> 
> URL: http://svn.apache.org/viewvc?rev=1411518&view=rev
> Log:
> Added the method org.apache.http.util.VersionInfo.setUserAgent(HttpParams, 
> String, String, Class<?>)
> TODO This needs adjusting to account for the deprecated types {@link 
> HttpParams} and {@link HttpProtocolParams}.
> 
> Modified:
>     
> httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/VersionInfo.java
> 
> Modified: 
> httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/VersionInfo.java
> URL: 
> http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/VersionInfo.java?rev=1411518&r1=1411517&r2=1411518&view=diff
> ==============================================================================
> --- 
> httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/VersionInfo.java
>  (original)
> +++ 
> httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/VersionInfo.java
>  Tue Nov 20 02:42:15 2012
> @@ -34,6 +34,9 @@ import java.util.List;
>  import java.util.Map;
>  import java.util.Properties;
>  
> +import org.apache.http.params.HttpParams;
> +import org.apache.http.params.HttpProtocolParams;
> +
>  
>  /**
>   * Provides access to version information for HTTP components.
> @@ -292,4 +295,29 @@ public class VersionInfo {
>          return new VersionInfo(pckg, module, release, timestamp, clsldrstr);
>      }
>  
> +     /**
> +      * Sets the user agent to {@code "<name>/<release> (Java 1.5 minimum; 
> Java/<java.version>)"}. 
> +      * <p/>
> +      * For example: 
> +      * <pre>"Apache-HttpClient/4.3 (Java 1.5 minimum; Java/1.6.0_35)"</pre>
> +      * 
> +      * TODO This needs adjusting to account for the deprecated types {@link 
> HttpParams} and {@link HttpProtocolParams}.
> +      * 
> +      * @param params
> +      *            the HTTP parameters where the user agent will be set
> +      * @name the component name, like "Apache-HttpClient".
> +      * @param pkg
> +      *            the package for which to load version information, for 
> example "org.apache.http". The package name
> +      *            should NOT end with a dot.
> +      * @param cls
> +      *            the class' class loader to load from, or 
> <code>null</code> for the thread context class loader
> +      */
> +     public static void setUserAgent(HttpParams params, String name, String 
> pkg, Class<?> cls) {
> +             // determine the release version from packaged version info
> +             final VersionInfo vi = VersionInfo.loadVersionInfo(pkg, 
> cls.getClassLoader());
> +             final String release = (vi != null) ? vi.getRelease() : 
> VersionInfo.UNAVAILABLE;
> +             final String javaVersion = System.getProperty("java.version");
> +             HttpProtocolParams.setUserAgent(params, name + "/" + release + 
> " (Java 1.5 minimum; Java/" + javaVersion + ")");
> +     }
> +
>  } // class VersionInfo
> 
> 

Gary,

I think there is no need for this method to depend on HttpParams or any
other configuration API. In my opinion it should simply return string.

Please note that System#getProperty method can be very treacherous.
Depending on the application security context it can throw unchecked
SecurityException. #setUserAgent should either catch the exception and
gracefully recover or explicitly declare SecurityException as thrown by
the method.

Oleg


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to