< prev index next >
src/share/classes/sun/security/timestamp/HttpTimestamper.java
Print this page
rev 1518 : 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
Reviewed-by: mchung
*** 26,42 ****
package sun.security.timestamp;
import java.io.BufferedInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.URL;
import java.net.HttpURLConnection;
! import java.util.List;
! import java.util.Map;
! import java.util.Set;
import sun.misc.IOUtils;
/**
* A timestamper that communicates with a Timestamping Authority (TSA)
* over HTTP.
* It supports the Time-Stamp Protocol defined in:
--- 26,42 ----
package sun.security.timestamp;
import java.io.BufferedInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
+ import java.net.URI;
import java.net.URL;
import java.net.HttpURLConnection;
! import java.util.*;
import sun.misc.IOUtils;
+ import sun.security.util.Debug;
/**
* A timestamper that communicates with a Timestamping Authority (TSA)
* over HTTP.
* It supports the Time-Stamp Protocol defined in:
*** 56,79 ****
// The MIME type for a timestamp reply
private static final String TS_REPLY_MIME_TYPE =
"application/timestamp-reply";
! private static final boolean DEBUG = false;
/*
! * HTTP URL identifying the location of the TSA
*/
! private String tsaUrl = null;
/**
* Creates a timestamper that connects to the specified TSA.
*
! * @param tsa The location of the TSA. It must be an HTTP URL.
*/
! public HttpTimestamper(String tsaUrl) {
! this.tsaUrl = tsaUrl;
}
/**
* Connects to the TSA and requests a timestamp.
*
--- 56,82 ----
// The MIME type for a timestamp reply
private static final String TS_REPLY_MIME_TYPE =
"application/timestamp-reply";
! private static final Debug debug = Debug.getInstance("ts");
/*
! * HTTP URI identifying the location of the TSA
*/
! private URI tsaURI = null;
/**
* Creates a timestamper that connects to the specified TSA.
*
! * @param tsa The location of the TSA. It must be an HTTP URI.
! * @throws IllegalArgumentException if tsaURI is not an HTTP URI
*/
! public HttpTimestamper(URI tsaURI) {
! if (!tsaURI.getScheme().equalsIgnoreCase("http"))
! throw new IllegalArgumentException("TSA must be an HTTP URI");
! this.tsaURI = tsaURI;
}
/**
* Connects to the TSA and requests a timestamp.
*
*** 83,121 ****
* communicating with the TSA.
*/
public TSResponse generateTimestamp(TSRequest tsQuery) throws IOException {
HttpURLConnection connection =
! (HttpURLConnection) new URL(tsaUrl).openConnection();
connection.setDoOutput(true);
connection.setUseCaches(false); // ignore cache
connection.setRequestProperty("Content-Type", TS_QUERY_MIME_TYPE);
connection.setRequestMethod("POST");
// Avoids the "hang" when a proxy is required but none has been set.
connection.setConnectTimeout(CONNECT_TIMEOUT);
! if (DEBUG) {
Set<Map.Entry<String, List<String>>> headers =
connection.getRequestProperties().entrySet();
! System.out.println(connection.getRequestMethod() + " " + tsaUrl +
" HTTP/1.1");
! for (Map.Entry<String, List<String>> entry : headers) {
! System.out.println(" " + entry);
}
! System.out.println();
}
connection.connect(); // No HTTP authentication is performed
// Send the request
DataOutputStream output = null;
try {
output = new DataOutputStream(connection.getOutputStream());
byte[] request = tsQuery.encode();
output.write(request, 0, request.length);
output.flush();
! if (DEBUG) {
! System.out.println("sent timestamp query (length=" +
request.length + ")");
}
} finally {
if (output != null) {
output.close();
--- 86,124 ----
* communicating with the TSA.
*/
public TSResponse generateTimestamp(TSRequest tsQuery) throws IOException {
HttpURLConnection connection =
! (HttpURLConnection) tsaURI.toURL().openConnection();
connection.setDoOutput(true);
connection.setUseCaches(false); // ignore cache
connection.setRequestProperty("Content-Type", TS_QUERY_MIME_TYPE);
connection.setRequestMethod("POST");
// Avoids the "hang" when a proxy is required but none has been set.
connection.setConnectTimeout(CONNECT_TIMEOUT);
! if (debug != null) {
Set<Map.Entry<String, List<String>>> headers =
connection.getRequestProperties().entrySet();
! debug.println(connection.getRequestMethod() + " " + tsaURI +
" HTTP/1.1");
! for (Map.Entry<String, List<String>> e : headers) {
! debug.println(" " + e);
}
! debug.println();
}
connection.connect(); // No HTTP authentication is performed
// Send the request
DataOutputStream output = null;
try {
output = new DataOutputStream(connection.getOutputStream());
byte[] request = tsQuery.encode();
output.write(request, 0, request.length);
output.flush();
! if (debug != null) {
! debug.println("sent timestamp query (length=" +
request.length + ")");
}
} finally {
if (output != null) {
output.close();
*** 125,152 ****
// Receive the reply
BufferedInputStream input = null;
byte[] replyBuffer = null;
try {
input = new BufferedInputStream(connection.getInputStream());
! if (DEBUG) {
String header = connection.getHeaderField(0);
! System.out.println(header);
int i = 1;
while ((header = connection.getHeaderField(i)) != null) {
String key = connection.getHeaderFieldKey(i);
! System.out.println(" " + ((key==null) ? "" : key + ": ") +
header);
i++;
}
! System.out.println();
}
int contentLength = connection.getContentLength();
verifyMimeType(connection.getContentType());
replyBuffer = IOUtils.readFully(input, contentLength, false);
! if (DEBUG) {
! System.out.println("received timestamp response (length=" +
replyBuffer.length + ")");
}
} finally {
if (input != null) {
input.close();
--- 128,155 ----
// Receive the reply
BufferedInputStream input = null;
byte[] replyBuffer = null;
try {
input = new BufferedInputStream(connection.getInputStream());
! if (debug != null) {
String header = connection.getHeaderField(0);
! debug.println(header);
int i = 1;
while ((header = connection.getHeaderField(i)) != null) {
String key = connection.getHeaderFieldKey(i);
! debug.println(" " + ((key==null) ? "" : key + ": ") +
header);
i++;
}
! debug.println();
}
int contentLength = connection.getContentLength();
verifyMimeType(connection.getContentType());
replyBuffer = IOUtils.readFully(input, contentLength, false);
! if (debug != null) {
! debug.println("received timestamp response (length=" +
replyBuffer.length + ")");
}
} finally {
if (input != null) {
input.close();
< prev index next >