< prev index next >

src/share/classes/sun/security/timestamp/HttpTimestamper.java

Print this page
rev 1387 : 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
Reviewed-by: xuelei, mullan
Contributed-by: alexandre.boulgakov@oracle.com
   1 /*
   2  * Copyright (c) 2003, 2009, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.security.timestamp;
  27 
  28 import java.io.BufferedInputStream;
  29 import java.io.DataOutputStream;
  30 import java.io.IOException;
  31 import java.net.URL;
  32 import java.net.HttpURLConnection;
  33 import java.util.Iterator;

  34 import java.util.Set;
  35 
  36 import sun.misc.IOUtils;
  37 import sun.security.pkcs.*;
  38 
  39 /**
  40  * A timestamper that communicates with a Timestamping Authority (TSA)
  41  * over HTTP.
  42  * It supports the Time-Stamp Protocol defined in:
  43  * <a href="http://www.ietf.org/rfc/rfc3161.txt">RFC 3161</a>.
  44  *
  45  * @since 1.5
  46  * @author Vincent Ryan
  47  */
  48 
  49 public class HttpTimestamper implements Timestamper {
  50 
  51     private static final int CONNECT_TIMEOUT = 15000; // 15 seconds
  52 
  53     // The MIME type for a timestamp query
  54     private static final String TS_QUERY_MIME_TYPE =
  55         "application/timestamp-query";
  56 
  57     // The MIME type for a timestamp reply


  77     /**
  78      * Connects to the TSA and requests a timestamp.
  79      *
  80      * @param tsQuery The timestamp query.
  81      * @return The result of the timestamp query.
  82      * @throws IOException The exception is thrown if a problem occurs while
  83      *         communicating with the TSA.
  84      */
  85     public TSResponse generateTimestamp(TSRequest tsQuery) throws IOException {
  86 
  87         HttpURLConnection connection =
  88             (HttpURLConnection) new URL(tsaUrl).openConnection();
  89         connection.setDoOutput(true);
  90         connection.setUseCaches(false); // ignore cache
  91         connection.setRequestProperty("Content-Type", TS_QUERY_MIME_TYPE);
  92         connection.setRequestMethod("POST");
  93         // Avoids the "hang" when a proxy is required but none has been set.
  94         connection.setConnectTimeout(CONNECT_TIMEOUT);
  95 
  96         if (DEBUG) {
  97             Set headers = connection.getRequestProperties().entrySet();

  98             System.out.println(connection.getRequestMethod() + " " + tsaUrl +
  99                 " HTTP/1.1");
 100             for (Iterator i = headers.iterator(); i.hasNext(); ) {
 101                 System.out.println("  " + i.next());
 102             }
 103             System.out.println();
 104         }
 105         connection.connect(); // No HTTP authentication is performed
 106 
 107         // Send the request
 108         DataOutputStream output = null;
 109         try {
 110             output = new DataOutputStream(connection.getOutputStream());
 111             byte[] request = tsQuery.encode();
 112             output.write(request, 0, request.length);
 113             output.flush();
 114             if (DEBUG) {
 115                 System.out.println("sent timestamp query (length=" +
 116                         request.length + ")");
 117             }
 118         } finally {
 119             if (output != null) {
 120                 output.close();
 121             }


   1 /*
   2  * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.security.timestamp;
  27 
  28 import java.io.BufferedInputStream;
  29 import java.io.DataOutputStream;
  30 import java.io.IOException;
  31 import java.net.URL;
  32 import java.net.HttpURLConnection;
  33 import java.util.List;
  34 import java.util.Map;
  35 import java.util.Set;
  36 
  37 import sun.misc.IOUtils;

  38 
  39 /**
  40  * A timestamper that communicates with a Timestamping Authority (TSA)
  41  * over HTTP.
  42  * It supports the Time-Stamp Protocol defined in:
  43  * <a href="http://www.ietf.org/rfc/rfc3161.txt">RFC 3161</a>.
  44  *
  45  * @since 1.5
  46  * @author Vincent Ryan
  47  */
  48 
  49 public class HttpTimestamper implements Timestamper {
  50 
  51     private static final int CONNECT_TIMEOUT = 15000; // 15 seconds
  52 
  53     // The MIME type for a timestamp query
  54     private static final String TS_QUERY_MIME_TYPE =
  55         "application/timestamp-query";
  56 
  57     // The MIME type for a timestamp reply


  77     /**
  78      * Connects to the TSA and requests a timestamp.
  79      *
  80      * @param tsQuery The timestamp query.
  81      * @return The result of the timestamp query.
  82      * @throws IOException The exception is thrown if a problem occurs while
  83      *         communicating with the TSA.
  84      */
  85     public TSResponse generateTimestamp(TSRequest tsQuery) throws IOException {
  86 
  87         HttpURLConnection connection =
  88             (HttpURLConnection) new URL(tsaUrl).openConnection();
  89         connection.setDoOutput(true);
  90         connection.setUseCaches(false); // ignore cache
  91         connection.setRequestProperty("Content-Type", TS_QUERY_MIME_TYPE);
  92         connection.setRequestMethod("POST");
  93         // Avoids the "hang" when a proxy is required but none has been set.
  94         connection.setConnectTimeout(CONNECT_TIMEOUT);
  95 
  96         if (DEBUG) {
  97             Set<Map.Entry<String, List<String>>> headers =
  98                     connection.getRequestProperties().entrySet();
  99             System.out.println(connection.getRequestMethod() + " " + tsaUrl +
 100                 " HTTP/1.1");
 101             for (Map.Entry<String, List<String>> entry : headers) {
 102                 System.out.println("  " + entry);
 103             }
 104             System.out.println();
 105         }
 106         connection.connect(); // No HTTP authentication is performed
 107 
 108         // Send the request
 109         DataOutputStream output = null;
 110         try {
 111             output = new DataOutputStream(connection.getOutputStream());
 112             byte[] request = tsQuery.encode();
 113             output.write(request, 0, request.length);
 114             output.flush();
 115             if (DEBUG) {
 116                 System.out.println("sent timestamp query (length=" +
 117                         request.length + ")");
 118             }
 119         } finally {
 120             if (output != null) {
 121                 output.close();
 122             }


< prev index next >