mirror of
https://github.com/haraldk/TwelveMonkeys.git
synced 2025-08-02 19:15:29 -04:00
Moved old obsolete stuff to sandbox.
This commit is contained in:
parent
4c18c2a685
commit
c5f1d8101b
@ -0,0 +1,202 @@
|
||||
/*
|
||||
* Copyright (c) 2013, Harald Kuhr
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name "TwelveMonkeys" nor the
|
||||
* names of its contributors may be used to endorse or promote products
|
||||
* derived from this software without 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, 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,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.twelvemonkeys.net;
|
||||
|
||||
import com.twelvemonkeys.lang.DateUtil;
|
||||
import com.twelvemonkeys.lang.StringUtil;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
|
||||
/**
|
||||
* HTTPUtil
|
||||
*
|
||||
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||
* @author last modified by $Author: haraldk$
|
||||
* @version $Id: HTTPUtil.java,v 1.0 08.09.13 13:57 haraldk Exp$
|
||||
*/
|
||||
public class HTTPUtil {
|
||||
/**
|
||||
* RFC 1123 date format, as recommended by RFC 2616 (HTTP/1.1), sec 3.3
|
||||
* NOTE: All date formats are private, to ensure synchronized access.
|
||||
*/
|
||||
private static final SimpleDateFormat HTTP_RFC1123_FORMAT = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
|
||||
static {
|
||||
HTTP_RFC1123_FORMAT.setTimeZone(TimeZone.getTimeZone("GMT"));
|
||||
}
|
||||
|
||||
/**
|
||||
* RFC 850 date format, (almost) as described in RFC 2616 (HTTP/1.1), sec 3.3
|
||||
* USE FOR PARSING ONLY (format is not 100% correct, to be more robust).
|
||||
*/
|
||||
private static final SimpleDateFormat HTTP_RFC850_FORMAT = new SimpleDateFormat("EEE, dd-MMM-yy HH:mm:ss z", Locale.US);
|
||||
/**
|
||||
* ANSI C asctime() date format, (almost) as described in RFC 2616 (HTTP/1.1), sec 3.3.
|
||||
* USE FOR PARSING ONLY (format is not 100% correct, to be more robust).
|
||||
*/
|
||||
private static final SimpleDateFormat HTTP_ASCTIME_FORMAT = new SimpleDateFormat("EEE MMM d HH:mm:ss yy", Locale.US);
|
||||
|
||||
private static long sNext50YearWindowChange = DateUtil.currentTimeDay();
|
||||
static {
|
||||
HTTP_RFC850_FORMAT.setTimeZone(TimeZone.getTimeZone("GMT"));
|
||||
HTTP_ASCTIME_FORMAT.setTimeZone(TimeZone.getTimeZone("GMT"));
|
||||
|
||||
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html#sec19.3:
|
||||
// - HTTP/1.1 clients and caches SHOULD assume that an RFC-850 date
|
||||
// which appears to be more than 50 years in the future is in fact
|
||||
// in the past (this helps solve the "year 2000" problem).
|
||||
update50YearWindowIfNeeded();
|
||||
}
|
||||
|
||||
private static void update50YearWindowIfNeeded() {
|
||||
// Avoid class synchronization
|
||||
long next = sNext50YearWindowChange;
|
||||
|
||||
if (next < System.currentTimeMillis()) {
|
||||
// Next check in one day
|
||||
next += DateUtil.DAY;
|
||||
sNext50YearWindowChange = next;
|
||||
|
||||
Date startDate = new Date(next - (50l * DateUtil.CALENDAR_YEAR));
|
||||
//System.out.println("next test: " + new Date(next) + ", 50 year start: " + startDate);
|
||||
synchronized (HTTP_RFC850_FORMAT) {
|
||||
HTTP_RFC850_FORMAT.set2DigitYearStart(startDate);
|
||||
}
|
||||
synchronized (HTTP_ASCTIME_FORMAT) {
|
||||
HTTP_ASCTIME_FORMAT.set2DigitYearStart(startDate);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats the time to a HTTP date, using the RFC 1123 format, as described
|
||||
* in <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3"
|
||||
* >RFC 2616 (HTTP/1.1), sec. 3.3</a>.
|
||||
*
|
||||
* @param pTime the time
|
||||
* @return a {@code String} representation of the time
|
||||
*/
|
||||
public static String formatHTTPDate(long pTime) {
|
||||
return formatHTTPDate(new Date(pTime));
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats the time to a HTTP date, using the RFC 1123 format, as described
|
||||
* in <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3"
|
||||
* >RFC 2616 (HTTP/1.1), sec. 3.3</a>.
|
||||
*
|
||||
* @param pTime the time
|
||||
* @return a {@code String} representation of the time
|
||||
*/
|
||||
public static String formatHTTPDate(Date pTime) {
|
||||
synchronized (HTTP_RFC1123_FORMAT) {
|
||||
return HTTP_RFC1123_FORMAT.format(pTime);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a HTTP date string into a {@code long} representing milliseconds
|
||||
* since January 1, 1970 GMT.
|
||||
* <p>
|
||||
* Use this method with headers that contain dates, such as
|
||||
* {@code If-Modified-Since} or {@code Last-Modified}.
|
||||
* <p>
|
||||
* The date string may be in either RFC 1123, RFC 850 or ANSI C asctime()
|
||||
* format, as described in
|
||||
* <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3"
|
||||
* >RFC 2616 (HTTP/1.1), sec. 3.3</a>
|
||||
*
|
||||
* @param pDate the date to parse
|
||||
*
|
||||
* @return a {@code long} value representing the date, expressed as the
|
||||
* number of milliseconds since January 1, 1970 GMT,
|
||||
* @throws NumberFormatException if the date parameter is not parseable.
|
||||
* @throws IllegalArgumentException if the date paramter is {@code null}
|
||||
*/
|
||||
public static long parseHTTPDate(String pDate) throws NumberFormatException {
|
||||
return parseHTTPDateImpl(pDate).getTime();
|
||||
}
|
||||
|
||||
/**
|
||||
* ParseHTTPDate implementation
|
||||
*
|
||||
* @param pDate the date string to parse
|
||||
*
|
||||
* @return a {@code Date}
|
||||
* @throws NumberFormatException if the date parameter is not parseable.
|
||||
* @throws IllegalArgumentException if the date paramter is {@code null}
|
||||
*/
|
||||
private static Date parseHTTPDateImpl(final String pDate) throws NumberFormatException {
|
||||
if (pDate == null) {
|
||||
throw new IllegalArgumentException("date == null");
|
||||
}
|
||||
|
||||
if (StringUtil.isEmpty(pDate)) {
|
||||
throw new NumberFormatException("Invalid HTTP date: \"" + pDate + "\"");
|
||||
}
|
||||
|
||||
DateFormat format;
|
||||
|
||||
if (pDate.indexOf('-') >= 0) {
|
||||
format = HTTP_RFC850_FORMAT;
|
||||
update50YearWindowIfNeeded();
|
||||
}
|
||||
else if (pDate.indexOf(',') < 0) {
|
||||
format = HTTP_ASCTIME_FORMAT;
|
||||
update50YearWindowIfNeeded();
|
||||
}
|
||||
else {
|
||||
format = HTTP_RFC1123_FORMAT;
|
||||
// NOTE: RFC1123 always uses 4-digit years
|
||||
}
|
||||
|
||||
Date date;
|
||||
try {
|
||||
//noinspection SynchronizationOnLocalVariableOrMethodParameter
|
||||
synchronized (format) {
|
||||
date = format.parse(pDate);
|
||||
}
|
||||
}
|
||||
catch (ParseException e) {
|
||||
NumberFormatException nfe = new NumberFormatException("Invalid HTTP date: \"" + pDate + "\"");
|
||||
nfe.initCause(e);
|
||||
throw nfe;
|
||||
}
|
||||
|
||||
if (date == null) {
|
||||
throw new NumberFormatException("Invalid HTTP date: \"" + pDate + "\"");
|
||||
}
|
||||
|
||||
return date;
|
||||
}
|
||||
}
|
@ -0,0 +1,83 @@
|
||||
/*
|
||||
* Copyright (c) 2013, Harald Kuhr
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name "TwelveMonkeys" nor the
|
||||
* names of its contributors may be used to endorse or promote products
|
||||
* derived from this software without 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, 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,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.twelvemonkeys.net;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* HTTPUtilTest
|
||||
*
|
||||
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||
* @author last modified by $Author: haraldk$
|
||||
* @version $Id: HTTPUtilTest.java,v 1.0 08.09.13 13:57 haraldk Exp$
|
||||
*/
|
||||
public class HTTPUtilTest {
|
||||
@Test
|
||||
public void testParseHTTPDateRFC1123() {
|
||||
long time = HTTPUtil.parseHTTPDate("Sun, 06 Nov 1994 08:49:37 GMT");
|
||||
assertEquals(784111777000l, time);
|
||||
|
||||
time = HTTPUtil.parseHTTPDate("Sunday, 06 Nov 1994 08:49:37 GMT");
|
||||
assertEquals(784111777000l, time);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseHTTPDateRFC850() {
|
||||
long time = HTTPUtil.parseHTTPDate("Sunday, 06-Nov-1994 08:49:37 GMT");
|
||||
assertEquals(784111777000l, time);
|
||||
|
||||
time = HTTPUtil.parseHTTPDate("Sun, 06-Nov-94 08:49:37 GMT");
|
||||
assertEquals(784111777000l, time);
|
||||
|
||||
// NOTE: This test will fail some time, around 2044,
|
||||
// as the 50 year window will slide...
|
||||
time = HTTPUtil.parseHTTPDate("Sunday, 06-Nov-94 08:49:37 GMT");
|
||||
assertEquals(784111777000l, time);
|
||||
|
||||
time = HTTPUtil.parseHTTPDate("Sun, 06-Nov-94 08:49:37 GMT");
|
||||
assertEquals(784111777000l, time);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseHTTPDateAsctime() {
|
||||
long time = HTTPUtil.parseHTTPDate("Sun Nov 6 08:49:37 1994");
|
||||
assertEquals(784111777000l, time);
|
||||
|
||||
time = HTTPUtil.parseHTTPDate("Sun Nov 6 08:49:37 94");
|
||||
assertEquals(784111777000l, time);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFormatHTTPDateRFC1123() {
|
||||
long time = 784111777000l;
|
||||
assertEquals("Sun, 06 Nov 1994 08:49:37 GMT", HTTPUtil.formatHTTPDate(time));
|
||||
}
|
||||
}
|
@ -1,59 +0,0 @@
|
||||
package com.twelvemonkeys.net;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* NetUtilTestCase
|
||||
* <p/>
|
||||
* <!-- To change this template use Options | File Templates. -->
|
||||
* <!-- Created by IntelliJ IDEA. -->
|
||||
*
|
||||
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||
* @version $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/test/java/com/twelvemonkeys/net/NetUtilTestCase.java#1 $
|
||||
*/
|
||||
public class NetUtilTestCase extends TestCase {
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
}
|
||||
|
||||
public void tearDown() throws Exception {
|
||||
super.tearDown();
|
||||
}
|
||||
|
||||
public void testParseHTTPDateRFC1123() {
|
||||
long time = NetUtil.parseHTTPDate("Sun, 06 Nov 1994 08:49:37 GMT");
|
||||
assertEquals(784111777000l, time);
|
||||
|
||||
time = NetUtil.parseHTTPDate("Sunday, 06 Nov 1994 08:49:37 GMT");
|
||||
assertEquals(784111777000l, time);
|
||||
}
|
||||
|
||||
public void testParseHTTPDateRFC850() {
|
||||
long time = NetUtil.parseHTTPDate("Sunday, 06-Nov-1994 08:49:37 GMT");
|
||||
assertEquals(784111777000l, time);
|
||||
|
||||
time = NetUtil.parseHTTPDate("Sun, 06-Nov-94 08:49:37 GMT");
|
||||
assertEquals(784111777000l, time);
|
||||
|
||||
// NOTE: This test will fail some time, around 2044,
|
||||
// as the 50 year window will slide...
|
||||
time = NetUtil.parseHTTPDate("Sunday, 06-Nov-94 08:49:37 GMT");
|
||||
assertEquals(784111777000l, time);
|
||||
|
||||
time = NetUtil.parseHTTPDate("Sun, 06-Nov-94 08:49:37 GMT");
|
||||
assertEquals(784111777000l, time);
|
||||
}
|
||||
|
||||
public void testParseHTTPDateAsctime() {
|
||||
long time = NetUtil.parseHTTPDate("Sun Nov 6 08:49:37 1994");
|
||||
assertEquals(784111777000l, time);
|
||||
|
||||
time = NetUtil.parseHTTPDate("Sun Nov 6 08:49:37 94");
|
||||
assertEquals(784111777000l, time);
|
||||
}
|
||||
|
||||
public void testFormatHTTPDateRFC1123() {
|
||||
long time = 784111777000l;
|
||||
assertEquals("Sun, 06 Nov 1994 08:49:37 GMT", NetUtil.formatHTTPDate(time));
|
||||
}
|
||||
}
|
@ -42,5 +42,4 @@ import java.net.*;
|
||||
*/
|
||||
public interface AuthenticatorFilter {
|
||||
public boolean accept(InetAddress pAddress, int pPort, String pProtocol, String pPrompt, String pScheme);
|
||||
|
||||
}
|
@ -44,7 +44,6 @@ import java.io.*;
|
||||
* @deprecated Use {@link com.twelvemonkeys.io.enc.Base64Encoder}/{@link Base64Decoder} instead
|
||||
*/
|
||||
class BASE64 {
|
||||
|
||||
/**
|
||||
* This array maps the characters to their 6 bit values
|
||||
*/
|
@ -42,7 +42,7 @@ import java.util.*;
|
||||
* <P/>
|
||||
* Note that the timeouts are created on the socket level, and that
|
||||
* <P/>
|
||||
* Note: This class should now work as expected, but it need more testing before
|
||||
* Note: This class should now work as expected, but it needs more testing before
|
||||
* it can enter production release.
|
||||
* <BR/>
|
||||
* --.k
|
||||
@ -55,7 +55,6 @@ import java.util.*;
|
||||
* @see <A href="http://www.w3.org/Protocols/rfc2616/rfc2616.html">RFC 2616</A>
|
||||
*/
|
||||
public class HttpURLConnection extends java.net.HttpURLConnection {
|
||||
|
||||
/**
|
||||
* HTTP Status-Code 307: Temporary Redirect
|
||||
*/
|
@ -2,15 +2,14 @@ package com.twelvemonkeys.net;
|
||||
|
||||
import com.twelvemonkeys.io.FileUtil;
|
||||
import com.twelvemonkeys.lang.StringUtil;
|
||||
import com.twelvemonkeys.lang.DateUtil;
|
||||
import com.twelvemonkeys.util.CollectionUtil;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.text.DateFormat;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* Utility class with network related methods.
|
||||
@ -74,58 +73,6 @@ public final class NetUtil {
|
||||
*/
|
||||
public final static String HTTP_TRACE = "TRACE";
|
||||
|
||||
/**
|
||||
* RFC 1123 date format, as reccomended by RFC 2616 (HTTP/1.1), sec 3.3
|
||||
* NOTE: All date formats are private, to ensure synchronized access.
|
||||
*/
|
||||
private static final SimpleDateFormat HTTP_RFC1123_FORMAT = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
|
||||
static {
|
||||
HTTP_RFC1123_FORMAT.setTimeZone(TimeZone.getTimeZone("GMT"));
|
||||
}
|
||||
|
||||
/**
|
||||
* RFC 850 date format, (allmost) as described in RFC 2616 (HTTP/1.1), sec 3.3
|
||||
* USE FOR PARSING ONLY (format is not 100% correct, to be more robust).
|
||||
*/
|
||||
private static final SimpleDateFormat HTTP_RFC850_FORMAT = new SimpleDateFormat("EEE, dd-MMM-yy HH:mm:ss z", Locale.US);
|
||||
/**
|
||||
* ANSI C asctime() date format, (allmost) as described in RFC 2616 (HTTP/1.1), sec 3.3.
|
||||
* USE FOR PARSING ONLY (format is not 100% correct, to be more robust).
|
||||
*/
|
||||
private static final SimpleDateFormat HTTP_ASCTIME_FORMAT = new SimpleDateFormat("EEE MMM d HH:mm:ss yy", Locale.US);
|
||||
|
||||
private static long sNext50YearWindowChange = DateUtil.currentTimeDay();
|
||||
static {
|
||||
HTTP_RFC850_FORMAT.setTimeZone(TimeZone.getTimeZone("GMT"));
|
||||
HTTP_ASCTIME_FORMAT.setTimeZone(TimeZone.getTimeZone("GMT"));
|
||||
|
||||
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html#sec19.3:
|
||||
// - HTTP/1.1 clients and caches SHOULD assume that an RFC-850 date
|
||||
// which appears to be more than 50 years in the future is in fact
|
||||
// in the past (this helps solve the "year 2000" problem).
|
||||
update50YearWindowIfNeeded();
|
||||
}
|
||||
|
||||
private static void update50YearWindowIfNeeded() {
|
||||
// Avoid class synchronization
|
||||
long next = sNext50YearWindowChange;
|
||||
|
||||
if (next < System.currentTimeMillis()) {
|
||||
// Next check in one day
|
||||
next += DateUtil.DAY;
|
||||
sNext50YearWindowChange = next;
|
||||
|
||||
Date startDate = new Date(next - (50l * DateUtil.CALENDAR_YEAR));
|
||||
//System.out.println("next test: " + new Date(next) + ", 50 year start: " + startDate);
|
||||
synchronized (HTTP_RFC850_FORMAT) {
|
||||
HTTP_RFC850_FORMAT.set2DigitYearStart(startDate);
|
||||
}
|
||||
synchronized (HTTP_ASCTIME_FORMAT) {
|
||||
HTTP_ASCTIME_FORMAT.set2DigitYearStart(startDate);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a NetUtil.
|
||||
* This class has only static methods and members, and should not be
|
||||
@ -134,15 +81,6 @@ public final class NetUtil {
|
||||
private NetUtil() {
|
||||
}
|
||||
|
||||
public static void main1(String[] args) {
|
||||
String timeStr = (args.length > 0 && !StringUtil.isNumber(args[0])) ? args[0] : null;
|
||||
|
||||
long time = args.length > 0 ?
|
||||
(timeStr != null ? parseHTTPDate(timeStr) : Long.parseLong(args[0]))
|
||||
: System.currentTimeMillis();
|
||||
System.out.println(timeStr + " --> " + time + " --> " + formatHTTPDate(time));
|
||||
}
|
||||
|
||||
/**
|
||||
* Main method, reads data from a URL and, optionally, writes it to stdout or a file.
|
||||
* @param pArgs command line arguemnts
|
||||
@ -253,7 +191,7 @@ public final class NetUtil {
|
||||
}
|
||||
}
|
||||
}
|
||||
java.net.HttpURLConnection conn;
|
||||
HttpURLConnection conn;
|
||||
|
||||
// Create connection
|
||||
URL reqURL = getURLAndSetAuthorization(url, requestProperties);
|
||||
@ -721,7 +659,7 @@ public final class NetUtil {
|
||||
* @see com.twelvemonkeys.net.HttpURLConnection
|
||||
* @see java.net.Socket
|
||||
* @see java.net.Socket#setSoTimeout(int) setSoTimeout
|
||||
* @see java.net.HttpURLConnection
|
||||
* @see HttpURLConnection
|
||||
* @see java.io.InterruptedIOException
|
||||
* @see <A href="http://www.w3.org/Protocols/rfc2616/rfc2616.html">RFC 2616</A>
|
||||
*/
|
||||
@ -768,7 +706,7 @@ public final class NetUtil {
|
||||
throws IOException {
|
||||
|
||||
// Open the connection, and get the stream
|
||||
java.net.HttpURLConnection conn = createHttpURLConnection(pURL, pProperties, pFollowRedirects, pTimeout);
|
||||
HttpURLConnection conn = createHttpURLConnection(pURL, pProperties, pFollowRedirects, pTimeout);
|
||||
|
||||
// HTTP GET method
|
||||
conn.setRequestMethod(HTTP_GET);
|
||||
@ -862,7 +800,7 @@ public final class NetUtil {
|
||||
public static InputStream getInputStreamHttpPost(URL pURL, Map pPostData, Properties pProperties, boolean pFollowRedirects, int pTimeout)
|
||||
throws IOException {
|
||||
// Open the connection, and get the stream
|
||||
java.net.HttpURLConnection conn = createHttpURLConnection(pURL, pProperties, pFollowRedirects, pTimeout);
|
||||
HttpURLConnection conn = createHttpURLConnection(pURL, pProperties, pFollowRedirects, pTimeout);
|
||||
|
||||
// HTTP POST method
|
||||
conn.setRequestMethod(HTTP_POST);
|
||||
@ -928,11 +866,11 @@ public final class NetUtil {
|
||||
* @throws UnknownHostException if the hostname in the URL cannot be found.
|
||||
* @throws IOException if an I/O exception occurs.
|
||||
*/
|
||||
public static java.net.HttpURLConnection createHttpURLConnection(URL pURL, Properties pProperties, boolean pFollowRedirects, int pTimeout)
|
||||
public static HttpURLConnection createHttpURLConnection(URL pURL, Properties pProperties, boolean pFollowRedirects, int pTimeout)
|
||||
throws IOException {
|
||||
|
||||
// Open the connection, and get the stream
|
||||
java.net.HttpURLConnection conn;
|
||||
HttpURLConnection conn;
|
||||
|
||||
if (pTimeout > 0) {
|
||||
// Supports timeout
|
||||
@ -940,7 +878,7 @@ public final class NetUtil {
|
||||
}
|
||||
else {
|
||||
// Faster, more compatible
|
||||
conn = (java.net.HttpURLConnection) pURL.openConnection();
|
||||
conn = (HttpURLConnection) pURL.openConnection();
|
||||
}
|
||||
|
||||
// Set user agent
|
||||
@ -966,7 +904,7 @@ public final class NetUtil {
|
||||
}
|
||||
catch (LinkageError le) {
|
||||
// This is the best we can do...
|
||||
java.net.HttpURLConnection.setFollowRedirects(pFollowRedirects);
|
||||
HttpURLConnection.setFollowRedirects(pFollowRedirects);
|
||||
System.err.println("You are using an old Java Spec, consider upgrading.");
|
||||
System.err.println("java.net.HttpURLConnection.setInstanceFollowRedirects(" + pFollowRedirects + ") failed.");
|
||||
|
||||
@ -1076,7 +1014,7 @@ public final class NetUtil {
|
||||
* @see com.twelvemonkeys.net.HttpURLConnection
|
||||
* @see java.net.Socket
|
||||
* @see java.net.Socket#setSoTimeout(int) setSoTimeout
|
||||
* @see java.net.HttpURLConnection
|
||||
* @see HttpURLConnection
|
||||
* @see java.io.InterruptedIOException
|
||||
* @see <A href="http://www.w3.org/Protocols/rfc2616/rfc2616.html">RFC 2616</A>
|
||||
*/
|
||||
@ -1317,106 +1255,4 @@ public final class NetUtil {
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats the time to a HTTP date, using the RFC 1123 format, as described
|
||||
* in <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3"
|
||||
* >RFC 2616 (HTTP/1.1), sec. 3.3</a>.
|
||||
*
|
||||
* @param pTime the time
|
||||
* @return a {@code String} representation of the time
|
||||
*/
|
||||
public static String formatHTTPDate(long pTime) {
|
||||
return formatHTTPDate(new Date(pTime));
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats the time to a HTTP date, using the RFC 1123 format, as described
|
||||
* in <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3"
|
||||
* >RFC 2616 (HTTP/1.1), sec. 3.3</a>.
|
||||
*
|
||||
* @param pTime the time
|
||||
* @return a {@code String} representation of the time
|
||||
*/
|
||||
public static String formatHTTPDate(Date pTime) {
|
||||
synchronized (HTTP_RFC1123_FORMAT) {
|
||||
return HTTP_RFC1123_FORMAT.format(pTime);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a HTTP date string into a {@code long} representing milliseconds
|
||||
* since January 1, 1970 GMT.
|
||||
* <p>
|
||||
* Use this method with headers that contain dates, such as
|
||||
* {@code If-Modified-Since} or {@code Last-Modified}.
|
||||
* <p>
|
||||
* The date string may be in either RFC 1123, RFC 850 or ANSI C asctime()
|
||||
* format, as described in
|
||||
* <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3"
|
||||
* >RFC 2616 (HTTP/1.1), sec. 3.3</a>
|
||||
*
|
||||
* @param pDate the date to parse
|
||||
*
|
||||
* @return a {@code long} value representing the date, expressed as the
|
||||
* number of milliseconds since January 1, 1970 GMT,
|
||||
* @throws NumberFormatException if the date parameter is not parseable.
|
||||
* @throws IllegalArgumentException if the date paramter is {@code null}
|
||||
*/
|
||||
public static long parseHTTPDate(String pDate) throws NumberFormatException {
|
||||
return parseHTTPDateImpl(pDate).getTime();
|
||||
}
|
||||
|
||||
/**
|
||||
* ParseHTTPDate implementation
|
||||
*
|
||||
* @param pDate the date string to parse
|
||||
*
|
||||
* @return a {@code Date}
|
||||
* @throws NumberFormatException if the date parameter is not parseable.
|
||||
* @throws IllegalArgumentException if the date paramter is {@code null}
|
||||
*/
|
||||
private static Date parseHTTPDateImpl(final String pDate) throws NumberFormatException {
|
||||
if (pDate == null) {
|
||||
throw new IllegalArgumentException("date == null");
|
||||
}
|
||||
|
||||
if (StringUtil.isEmpty(pDate)) {
|
||||
throw new NumberFormatException("Invalid HTTP date: \"" + pDate + "\"");
|
||||
}
|
||||
|
||||
DateFormat format;
|
||||
|
||||
if (pDate.indexOf('-') >= 0) {
|
||||
format = HTTP_RFC850_FORMAT;
|
||||
update50YearWindowIfNeeded();
|
||||
}
|
||||
else if (pDate.indexOf(',') < 0) {
|
||||
format = HTTP_ASCTIME_FORMAT;
|
||||
update50YearWindowIfNeeded();
|
||||
}
|
||||
else {
|
||||
format = HTTP_RFC1123_FORMAT;
|
||||
// NOTE: RFC1123 always uses 4-digit years
|
||||
}
|
||||
|
||||
Date date;
|
||||
try {
|
||||
//noinspection SynchronizationOnLocalVariableOrMethodParameter
|
||||
synchronized (format) {
|
||||
date = format.parse(pDate);
|
||||
}
|
||||
}
|
||||
catch (ParseException e) {
|
||||
NumberFormatException nfe = new NumberFormatException("Invalid HTTP date: \"" + pDate + "\"");
|
||||
nfe.initCause(e);
|
||||
throw nfe;
|
||||
}
|
||||
|
||||
if (date == null) {
|
||||
throw new NumberFormatException("Invalid HTTP date: \"" + pDate + "\"");
|
||||
}
|
||||
|
||||
return date;
|
||||
}
|
||||
}
|
@ -36,7 +36,7 @@ import java.net.*;
|
||||
* @see SimpleAuthenticator
|
||||
* @see java.net.Authenticator
|
||||
*
|
||||
* @author Harald Kuhr (haraldk@iconmedialab.no)
|
||||
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||
*
|
||||
* @version 1.0
|
||||
*/
|
@ -47,12 +47,11 @@ import java.util.Map;
|
||||
* once more, an idea came to my mind. This is the result. I hope you find it
|
||||
* useful. -- Harald K.</EM>
|
||||
*
|
||||
* @author Harald Kuhr (haraldk@iconmedialab.no)
|
||||
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||
* @version 1.0
|
||||
* @see java.net.Authenticator
|
||||
*/
|
||||
public class SimpleAuthenticator extends Authenticator {
|
||||
|
||||
/** The reference to the single instance of this class. */
|
||||
private static SimpleAuthenticator sInstance = null;
|
||||
/** Keeps track of the state of this class. */
|
||||
@ -211,6 +210,7 @@ public class SimpleAuthenticator extends Authenticator {
|
||||
* Everything but address may be null
|
||||
*/
|
||||
class AuthKey {
|
||||
// TODO: Move this class to sandbox?
|
||||
|
||||
InetAddress address = null;
|
||||
int port = -1;
|
@ -29,7 +29,7 @@
|
||||
package com.twelvemonkeys.servlet.cache;
|
||||
|
||||
import com.twelvemonkeys.lang.StringUtil;
|
||||
import com.twelvemonkeys.net.NetUtil;
|
||||
import com.twelvemonkeys.net.HTTPUtil;
|
||||
import com.twelvemonkeys.servlet.ServletResponseStreamDelegate;
|
||||
|
||||
import javax.servlet.ServletOutputStream;
|
||||
@ -212,7 +212,7 @@ class CacheResponseWrapper extends HttpServletResponseWrapper {
|
||||
if (Boolean.FALSE.equals(cacheable)) {
|
||||
super.setDateHeader(pName, pValue);
|
||||
}
|
||||
cachedResponse.setHeader(pName, NetUtil.formatHTTPDate(pValue));
|
||||
cachedResponse.setHeader(pName, HTTPUtil.formatHTTPDate(pValue));
|
||||
}
|
||||
|
||||
public void addDateHeader(String pName, long pValue) {
|
||||
@ -220,7 +220,7 @@ class CacheResponseWrapper extends HttpServletResponseWrapper {
|
||||
if (Boolean.FALSE.equals(cacheable)) {
|
||||
super.addDateHeader(pName, pValue);
|
||||
}
|
||||
cachedResponse.addHeader(pName, NetUtil.formatHTTPDate(pValue));
|
||||
cachedResponse.addHeader(pName, HTTPUtil.formatHTTPDate(pValue));
|
||||
}
|
||||
|
||||
public void setHeader(String pName, String pValue) {
|
||||
|
@ -32,7 +32,7 @@ import com.twelvemonkeys.io.FileUtil;
|
||||
import com.twelvemonkeys.lang.StringUtil;
|
||||
import com.twelvemonkeys.lang.Validate;
|
||||
import com.twelvemonkeys.net.MIMEUtil;
|
||||
import com.twelvemonkeys.net.NetUtil;
|
||||
import com.twelvemonkeys.net.HTTPUtil;
|
||||
import com.twelvemonkeys.util.LRUHashMap;
|
||||
import com.twelvemonkeys.util.NullMap;
|
||||
|
||||
@ -972,7 +972,7 @@ public class HTTPCache {
|
||||
File cached = getCachedFile(pCacheURI, pRequest);
|
||||
if (cached != null && cached.exists()) {
|
||||
lastModified = cached.lastModified();
|
||||
//// System.out.println(" ## HTTPCache ## Last-Modified is " + NetUtil.formatHTTPDate(lastModified) + ", using cachedFile.lastModified()");
|
||||
//// System.out.println(" ## HTTPCache ## Last-Modified is " + HTTPUtil.formatHTTPDate(lastModified) + ", using cachedFile.lastModified()");
|
||||
}
|
||||
}
|
||||
*/
|
||||
@ -981,11 +981,11 @@ public class HTTPCache {
|
||||
int maxAge = getIntHeader(response, HEADER_CACHE_CONTROL, "max-age");
|
||||
if (maxAge == -1) {
|
||||
expires = lastModified + defaultExpiryTime;
|
||||
//// System.out.println(" ## HTTPCache ## Expires is " + NetUtil.formatHTTPDate(expires) + ", using lastModified + defaultExpiry");
|
||||
//// System.out.println(" ## HTTPCache ## Expires is " + HTTPUtil.formatHTTPDate(expires) + ", using lastModified + defaultExpiry");
|
||||
}
|
||||
else {
|
||||
expires = lastModified + (maxAge * 1000L); // max-age is seconds
|
||||
//// System.out.println(" ## HTTPCache ## Expires is " + NetUtil.formatHTTPDate(expires) + ", using lastModified + maxAge");
|
||||
//// System.out.println(" ## HTTPCache ## Expires is " + HTTPUtil.formatHTTPDate(expires) + ", using lastModified + maxAge");
|
||||
}
|
||||
}
|
||||
/*
|
||||
@ -997,7 +997,7 @@ public class HTTPCache {
|
||||
// Expired?
|
||||
if (expires < now) {
|
||||
// System.out.println(" ## HTTPCache ## Content is stale (content expired: "
|
||||
// + NetUtil.formatHTTPDate(expires) + " before " + NetUtil.formatHTTPDate(now) + ").");
|
||||
// + HTTPUtil.formatHTTPDate(expires) + " before " + HTTPUtil.formatHTTPDate(now) + ").");
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -1008,7 +1008,7 @@ public class HTTPCache {
|
||||
File cached = getCachedFile(pCacheURI, pRequest);
|
||||
if (cached != null && cached.exists()) {
|
||||
lastModified = cached.lastModified();
|
||||
//// System.out.println(" ## HTTPCache ## Last-Modified is " + NetUtil.formatHTTPDate(lastModified) + ", using cachedFile.lastModified()");
|
||||
//// System.out.println(" ## HTTPCache ## Last-Modified is " + HTTPUtil.formatHTTPDate(lastModified) + ", using cachedFile.lastModified()");
|
||||
}
|
||||
}
|
||||
*/
|
||||
@ -1018,7 +1018,7 @@ public class HTTPCache {
|
||||
//noinspection RedundantIfStatement
|
||||
if (real != null && real.exists() && real.lastModified() > lastModified) {
|
||||
// System.out.println(" ## HTTPCache ## Content is stale (new content"
|
||||
// + NetUtil.formatHTTPDate(lastModified) + " before " + NetUtil.formatHTTPDate(real.lastModified()) + ").");
|
||||
// + HTTPUtil.formatHTTPDate(lastModified) + " before " + HTTPUtil.formatHTTPDate(real.lastModified()) + ").");
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -1082,7 +1082,7 @@ public class HTTPCache {
|
||||
static long getDateHeader(final String pHeaderValue) {
|
||||
long date = -1L;
|
||||
if (pHeaderValue != null) {
|
||||
date = NetUtil.parseHTTPDate(pHeaderValue);
|
||||
date = HTTPUtil.parseHTTPDate(pHeaderValue);
|
||||
}
|
||||
return date;
|
||||
}
|
||||
|
@ -29,7 +29,7 @@
|
||||
package com.twelvemonkeys.servlet.cache;
|
||||
|
||||
import com.twelvemonkeys.io.FastByteArrayOutputStream;
|
||||
import com.twelvemonkeys.net.NetUtil;
|
||||
import com.twelvemonkeys.net.HTTPUtil;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
@ -53,7 +53,7 @@ class WritableCachedResponseImpl implements WritableCachedResponse {
|
||||
protected WritableCachedResponseImpl() {
|
||||
cachedResponse = new CachedResponseImpl();
|
||||
// Hmmm..
|
||||
setHeader(HTTPCache.HEADER_CACHED_TIME, NetUtil.formatHTTPDate(System.currentTimeMillis()));
|
||||
setHeader(HTTPCache.HEADER_CACHED_TIME, HTTPUtil.formatHTTPDate(System.currentTimeMillis()));
|
||||
}
|
||||
|
||||
public CachedResponse getCachedResponse() {
|
||||
|
@ -1,6 +1,6 @@
|
||||
package com.twelvemonkeys.servlet.cache;
|
||||
|
||||
import com.twelvemonkeys.net.NetUtil;
|
||||
import com.twelvemonkeys.net.HTTPUtil;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
@ -644,7 +644,7 @@ public class HTTPCacheTestCase {
|
||||
CacheResponse res = (CacheResponse) invocation.getArguments()[1];
|
||||
|
||||
res.setStatus(HttpServletResponse.SC_OK);
|
||||
res.setHeader("Date", NetUtil.formatHTTPDate(System.currentTimeMillis()));
|
||||
res.setHeader("Date", HTTPUtil.formatHTTPDate(System.currentTimeMillis()));
|
||||
res.setHeader("Cache-Control", "public");
|
||||
res.addHeader("X-Custom", "FOO");
|
||||
res.addHeader("X-Custom", "BAR");
|
||||
@ -1126,7 +1126,7 @@ public class HTTPCacheTestCase {
|
||||
CacheResponse res = (CacheResponse) invocation.getArguments()[1];
|
||||
|
||||
res.setStatus(status);
|
||||
res.setHeader("Date", NetUtil.formatHTTPDate(System.currentTimeMillis()));
|
||||
res.setHeader("Date", HTTPUtil.formatHTTPDate(System.currentTimeMillis()));
|
||||
|
||||
for (Map.Entry<String, List<String>> header : headers.entrySet()) {
|
||||
for (String value : header.getValue()) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user