src/core/net/sf/basedb/util/HttpUtil.java

Code
Comments
Other
Rev Date Author Line
5360 11 Jun 10 nicklas 1 /*
5360 11 Jun 10 nicklas 2   $Id $
5360 11 Jun 10 nicklas 3
5360 11 Jun 10 nicklas 4   Copyright (C) 2010 Nicklas Nordborg
5360 11 Jun 10 nicklas 5
5360 11 Jun 10 nicklas 6   This file is part of BASE - BioArray Software Environment.
5360 11 Jun 10 nicklas 7   Available at http://base.thep.lu.se/
5360 11 Jun 10 nicklas 8
5360 11 Jun 10 nicklas 9   BASE is free software; you can redistribute it and/or
5360 11 Jun 10 nicklas 10   modify it under the terms of the GNU General Public License
5360 11 Jun 10 nicklas 11   as published by the Free Software Foundation; either version 3
5360 11 Jun 10 nicklas 12   of the License, or (at your option) any later version.
5360 11 Jun 10 nicklas 13
5360 11 Jun 10 nicklas 14   BASE is distributed in the hope that it will be useful,
5360 11 Jun 10 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
5360 11 Jun 10 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5360 11 Jun 10 nicklas 17   GNU General Public License for more details.
5360 11 Jun 10 nicklas 18
5360 11 Jun 10 nicklas 19   You should have received a copy of the GNU General Public License
5360 11 Jun 10 nicklas 20   along with BASE. If not, see <http://www.gnu.org/licenses/>.
5360 11 Jun 10 nicklas 21 */
5360 11 Jun 10 nicklas 22 package net.sf.basedb.util;
5360 11 Jun 10 nicklas 23
6477 12 Jun 14 nicklas 24 import java.io.IOException;
5360 11 Jun 10 nicklas 25 import java.util.Date;
5360 11 Jun 10 nicklas 26 import java.util.Locale;
5360 11 Jun 10 nicklas 27
8115 13 Feb 23 nicklas 28 import org.apache.commons.lang3.time.FastDateFormat;
5360 11 Jun 10 nicklas 29 import org.apache.http.Header;
5360 11 Jun 10 nicklas 30 import org.apache.http.HttpResponse;
6477 12 Jun 14 nicklas 31 import org.apache.http.impl.client.CloseableHttpClient;
5360 11 Jun 10 nicklas 32
7411 10 Oct 17 nicklas 33 import nl.basjes.parse.useragent.UserAgent;
7411 10 Oct 17 nicklas 34 import nl.basjes.parse.useragent.UserAgentAnalyzer;
7411 10 Oct 17 nicklas 35
5360 11 Jun 10 nicklas 36 /**
5360 11 Jun 10 nicklas 37   Useful methods related to HTTP stuff in general and
5360 11 Jun 10 nicklas 38   Apache HTTP components in particular.
5360 11 Jun 10 nicklas 39   
5360 11 Jun 10 nicklas 40   @author Nicklas
5360 11 Jun 10 nicklas 41   @since 2.16
5360 11 Jun 10 nicklas 42   @base.modified $Date$
5360 11 Jun 10 nicklas 43 */
5360 11 Jun 10 nicklas 44 public class HttpUtil 
5360 11 Jun 10 nicklas 45 {
5360 11 Jun 10 nicklas 46   
8115 13 Feb 23 nicklas 47   private static final FastDateFormat HTTP_DATE_FORMAT = FastDateFormat.getInstance("EEE, d MMM yyyy HH:mm:ss z", Locale.ENGLISH);
5360 11 Jun 10 nicklas 48
7411 10 Oct 17 nicklas 49   private static UserAgentAnalyzer userAgentAnalyzer;
7411 10 Oct 17 nicklas 50   
5360 11 Jun 10 nicklas 51   /**
5360 11 Jun 10 nicklas 52     Get the content length from the headers in the response.
5360 11 Jun 10 nicklas 53     @param response The response from a HTTP request
5360 11 Jun 10 nicklas 54     @return The value of the "Content-Length" header or -1 if not present 
5360 11 Jun 10 nicklas 55       if it is unparsable
5360 11 Jun 10 nicklas 56   */
5360 11 Jun 10 nicklas 57   public static long getContentLength(HttpResponse response)
5360 11 Jun 10 nicklas 58   {
5360 11 Jun 10 nicklas 59     return getLong(response, "Content-Length", -1L);
5360 11 Jun 10 nicklas 60   }
5360 11 Jun 10 nicklas 61
5360 11 Jun 10 nicklas 62   /**
5360 11 Jun 10 nicklas 63     Get the date the content was last modified from the headers 
5360 11 Jun 10 nicklas 64     in the response.
5360 11 Jun 10 nicklas 65     @param response The response from a HTTP request
5360 11 Jun 10 nicklas 66     @return The value of the "Last-Modified" header or null if not present 
5360 11 Jun 10 nicklas 67       if it is unparsable
5360 11 Jun 10 nicklas 68   */
5360 11 Jun 10 nicklas 69   public static Date getLastModified(HttpResponse response)
5360 11 Jun 10 nicklas 70   {
5360 11 Jun 10 nicklas 71     return getDate(response, "Last-Modified", null);
5360 11 Jun 10 nicklas 72   }
5360 11 Jun 10 nicklas 73
5360 11 Jun 10 nicklas 74   /**
5360 11 Jun 10 nicklas 75     Get the content type of the content from the headers 
5360 11 Jun 10 nicklas 76     in the response.
5360 11 Jun 10 nicklas 77     @param response The response from a HTTP request
5360 11 Jun 10 nicklas 78     @return The value of the "Content-Type" header or null if not present 
5360 11 Jun 10 nicklas 79       if it is unparsable
5360 11 Jun 10 nicklas 80   */
5360 11 Jun 10 nicklas 81   public static String getContentType(HttpResponse response)
5360 11 Jun 10 nicklas 82   {
5360 11 Jun 10 nicklas 83     return getString(response, "Content-Type", null);
5360 11 Jun 10 nicklas 84   }
5360 11 Jun 10 nicklas 85
5360 11 Jun 10 nicklas 86   
5360 11 Jun 10 nicklas 87   public static String getString(HttpResponse response, String header, String defaultValue)
5360 11 Jun 10 nicklas 88   {
5360 11 Jun 10 nicklas 89     String result = defaultValue;
5360 11 Jun 10 nicklas 90     Header h = response.getFirstHeader(header);
5360 11 Jun 10 nicklas 91     if (h != null) result = h.getValue();
5360 11 Jun 10 nicklas 92     return result;
5360 11 Jun 10 nicklas 93   }
5360 11 Jun 10 nicklas 94   
5360 11 Jun 10 nicklas 95   public static Long getLong(HttpResponse response, String header, Long defaultValue)
5360 11 Jun 10 nicklas 96   {
5360 11 Jun 10 nicklas 97     Long result = defaultValue;
5360 11 Jun 10 nicklas 98     Header h = response.getFirstHeader(header);
5360 11 Jun 10 nicklas 99     if (h != null) result = Values.getLong(h.getValue(), defaultValue);
5360 11 Jun 10 nicklas 100     return result;
5360 11 Jun 10 nicklas 101   }
5360 11 Jun 10 nicklas 102   
5360 11 Jun 10 nicklas 103   public static Date getDate(HttpResponse response, String header, Date defaultValue)
5360 11 Jun 10 nicklas 104   {
5360 11 Jun 10 nicklas 105     Date result = defaultValue;
5360 11 Jun 10 nicklas 106     Header h = response.getFirstHeader(header);
5360 11 Jun 10 nicklas 107     if (h != null) 
5360 11 Jun 10 nicklas 108     {
5360 11 Jun 10 nicklas 109       try 
5360 11 Jun 10 nicklas 110       { 
5360 11 Jun 10 nicklas 111         result = HTTP_DATE_FORMAT.parse(h.getValue());
5360 11 Jun 10 nicklas 112       }
5360 11 Jun 10 nicklas 113       catch (Exception ex) 
5360 11 Jun 10 nicklas 114       {}
5360 11 Jun 10 nicklas 115     }
5360 11 Jun 10 nicklas 116     return result;
5360 11 Jun 10 nicklas 117   }
5360 11 Jun 10 nicklas 118   
5360 11 Jun 10 nicklas 119   /**
5360 11 Jun 10 nicklas 120     Safely shuts down a http client and it's connection manager
5360 11 Jun 10 nicklas 121     without trowing an exception. Useful to use in try-catch-finally
5360 11 Jun 10 nicklas 122     clauses.
5360 11 Jun 10 nicklas 123   */
6477 12 Jun 14 nicklas 124   public static void shutdown(CloseableHttpClient client)
6477 12 Jun 14 nicklas 125   {
6477 12 Jun 14 nicklas 126     try
6477 12 Jun 14 nicklas 127     {
6477 12 Jun 14 nicklas 128       client.close();
6477 12 Jun 14 nicklas 129     }
6477 12 Jun 14 nicklas 130     catch (IOException ex)
6477 12 Jun 14 nicklas 131     {}
6477 12 Jun 14 nicklas 132     catch (RuntimeException ex)
6477 12 Jun 14 nicklas 133     {}
6477 12 Jun 14 nicklas 134   }
5360 11 Jun 10 nicklas 135   
7411 10 Oct 17 nicklas 136   /**
7411 10 Oct 17 nicklas 137     Parse the User-Agent string from a HTTP request and return
7411 10 Oct 17 nicklas 138     information about what it actually means.
7461 14 Mar 18 nicklas 139     @see <a href="https://github.com/nielsbasjes/yauaa">https://github.com/nielsbasjes/yauaa</a>
7411 10 Oct 17 nicklas 140     @since 3.12
7411 10 Oct 17 nicklas 141   */
7411 10 Oct 17 nicklas 142   public static synchronized UserAgent analyzeUserAgent(String userAgent)
7411 10 Oct 17 nicklas 143   {
7411 10 Oct 17 nicklas 144     if (userAgentAnalyzer == null)
7411 10 Oct 17 nicklas 145     {
7411 10 Oct 17 nicklas 146       userAgentAnalyzer = UserAgentAnalyzer.newBuilder().build();
7411 10 Oct 17 nicklas 147     }
7411 10 Oct 17 nicklas 148     return userAgentAnalyzer.parse(userAgent);
7411 10 Oct 17 nicklas 149   }
7411 10 Oct 17 nicklas 150   
7411 10 Oct 17 nicklas 151   /**
7411 10 Oct 17 nicklas 152     Get a summary of the user agent:
7411 10 Oct 17 nicklas 153     
7411 10 Oct 17 nicklas 154     * Browser with major version
7411 10 Oct 17 nicklas 155     * Operating system with version
7411 10 Oct 17 nicklas 156     * Typy of device (eg. desktop, phone, etc.)
7411 10 Oct 17 nicklas 157     
7411 10 Oct 17 nicklas 158     @since 3.12
7411 10 Oct 17 nicklas 159   */
7411 10 Oct 17 nicklas 160   public static String getSummaryOfUserAgent(String userAgent)
7411 10 Oct 17 nicklas 161   {
7411 10 Oct 17 nicklas 162     UserAgent ua = analyzeUserAgent(userAgent);
7411 10 Oct 17 nicklas 163     return ua == null ? null : ua.getValue("AgentNameVersionMajor") + 
7411 10 Oct 17 nicklas 164         " on " + ua.getValue("OperatingSystemNameVersion") +
7411 10 Oct 17 nicklas 165         " (" + ua.getValue("DeviceName") + ")";
7411 10 Oct 17 nicklas 166   }
7411 10 Oct 17 nicklas 167   
5360 11 Jun 10 nicklas 168 }