Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/enhydra/httpServerTest/common/HttpServerTestUtils.java


1   /*
2    * Enhydra Java Application Server Project
3    * 
4    * The contents of this file are subject to the Enhydra Public License
5    * Version 1.1 (the "License"); you may not use this file except in
6    * compliance with the License. You may obtain a copy of the License on
7    * the Enhydra web site ( http://www.enhydra.org/ ).
8    * 
9    * Software distributed under the License is distributed on an "AS IS"
10   * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 
11   * the License for the specific terms governing rights and limitations
12   * under the License.
13   * 
14   * The Initial Developer of the Enhydra Application Server is Lutris
15   * Technologies, Inc. The Enhydra Application Server and portions created
16   * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17   * All Rights Reserved.
18   * 
19   * Contributor(s):
20   * 
21   * $Id: HttpServerTestUtils.java,v 1.5.2.1.2.1 2000/10/19 17:59:08 jasona Exp $
22   */
23  
24  package org.enhydra.httpServerTest.common;
25  
26  import com.lutris.util.Base64Encoder;
27  
28  import java.io.File;
29  import java.io.FileInputStream;
30  import java.security.MessageDigest;
31  
32  /**
33   * This class provides static data and methods that are shared by both the 
34   * client and server sides of this test.  See below for details.
35   *
36   * @author Mike Ward
37   **/
38  public class HttpServerTestUtils {
39      /**
40       * The string designating a session cookie.
41       **/
42      public static final String HTTP_COOKIE_SESSION_KEY = "JSESSIONID";
43  
44      /**
45       * The string designating a session CGI parameter.
46       **/
47      public static final String CGI_PARAM_SESSION_KEY = "jsessionid";
48  
49      /**
50       * The string representing an http GET request method.
51       **/
52      public static final String GET_REQUEST_TAG = "GET";
53  
54      /**
55       * The string representing an http POST request method.
56       **/
57      public static final String POST_REQUEST_TAG = "POST";
58  
59      /**
60       * This string is used by the server side components to indicate the
61       * type of post method that was used.  Requests submitted using this
62       * header can hava all CGI arguments found in the same manner as a GET
63       * request (from the servlet's getParameter*() methods).  Otherwise,
64       * the request consists of a multi-part mime and must be decoded.
65       **/
66      public static final String POST_URL_ENCODED_CONTENT_TYPE = 
67    "application/x-www-form-urlencoded";
68  
69      /**
70       * This is the name used to designate a file upload within a multi-
71       * part mime message.
72       **/
73      public static final String POST_FILE_UPLOAD_PREFIX = "fileUpload";
74  
75      /**
76       * This is an array of headers commonly set automatically by HTTPClient
77       * when issueing request.
78       * @see HTTPClient.HTTPConnection
79       **/
80      public static final String[] defaulHeaderArray = {"Connection", 
81                    "Content-type",
82                    "Content-length",
83                    "Accept-Encoding", 
84                    "Cookie", 
85                    "Cookie2", 
86                    "User-Agent", 
87                    "Host", 
88                    "TE"};
89      
90      /**
91       * This fuction will loop through the 'defaultHeaderArray' and return
92       * true if the supplied header is considered 'default' and false
93       * otherwise.
94       * 
95       * @param headerName
96       *        The header name to check against defaults.
97       * @return boolean
98       *         Returns true if header is found, false otherwise.
99       **/
100     public static boolean isDefaultHeader(String headerName) {
101   for (int i = 0; i < defaulHeaderArray.length; i++) {
102       if (defaulHeaderArray[i].equals(headerName)) {
103     return true;
104       }
105   }
106   return false;
107     }
108 
109     /**
110      * This function will calculate an MD5 hashkey for the given file name.
111      * The hashkey is Base64 encoded for safe transport over the connection.
112      *
113      * @see com.lutris.util.Base64Encoder
114      *
115      * @param fileName
116      *        The file name to compute hashkey for.
117      * @return String
118      *         The Base64 encoded hashkey for this file.
119      * @exception Exception
120      *            Thrown if an error ocurs while reading file and/or computing
121      *            the file's hashkey value.
122      **/
123     public static String calculateFileHash(String fileName) 
124   throws Exception {
125   try {
126       File file = new File(fileName);
127       if (!file.exists() ||
128     !file.canRead()) {
129     return null;
130       }
131       
132       MessageDigest messageDigest = MessageDigest.getInstance("MD5");
133 
134       FileInputStream input = new FileInputStream(file);
135       byte[] bytes = new byte[1024];
136       int bytesRead = input.read(bytes);
137       while (bytesRead > 0) {
138     messageDigest.update(bytes, 0, bytesRead);
139     bytesRead = input.read(bytes);
140       }
141       input.close();
142 
143       return Base64Encoder.toBase64SessionKeyString(messageDigest.digest());
144   } catch (Exception e) {
145       throw e;
146   }
147     }
148 }