KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > util > FileUploadUtil


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.deployment.util;
25
26 import java.io.*;
27 import java.net.*;
28 import java.util.*;
29
30 import sun.misc.BASE64Encoder;
31
32 import com.sun.enterprise.deployment.deploy.shared.Archive;
33 import com.sun.enterprise.deployment.deploy.shared.MemoryMappedArchive;
34
35
36 public class FileUploadUtil {
37     private static final String JavaDoc JSR88 = "jsr88";
38
39     /*
40      * Alternate upload method. It will use UploadServlet instead of using JMX MBean.
41      * Reason is to increase upload speed.
42      * @param host admin hostname
43      * @param port admin port
44      * @param user admin username
45      * @param password admin password
46      * @param file upload file path
47      * @param name moduleId
48      * @return remote file path
49     */

50     public static String JavaDoc uploadToServlet(String JavaDoc host, String JavaDoc port, String JavaDoc user,
51         String JavaDoc password, Archive module) throws Exception JavaDoc {
52
53         Socket socket = null;
54         BufferedInputStream bis = null;
55
56         try {
57
58             String JavaDoc fileName = null;
59             if (module instanceof MemoryMappedArchive) {
60                 // jsr88: module.getArchiveUri() == null
61
byte[] bytes = ((MemoryMappedArchive) module).getByteArray();
62                 bis = new BufferedInputStream(new ByteArrayInputStream(bytes));
63                 File tmpFile = File.createTempFile(JSR88, null);
64                 tmpFile.deleteOnExit();
65                 fileName = tmpFile.getName();
66             } else {
67                 // other deployment mechanisms
68
File f = new File(module.getURI().getPath());
69                 bis = new BufferedInputStream(new FileInputStream(f));
70                 fileName = f.getName();
71             }
72
73             
74             // upload servlet url
75
String JavaDoc path = "/web1/uploadServlet?file=" + URLEncoder.encode(fileName, "UTF-8");
76
77             socket = new Socket(host, Integer.parseInt(port));
78
79             OutputStream out = socket.getOutputStream();
80             InputStream in = socket.getInputStream();
81
82             // write HTTP headers
83
BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
84             wr.write("POST " + path + " HTTP/1.0\r\n");
85             wr.write("Content-Length: " + bis.available() + "\r\n");
86             wr.write("Content-Type: application/octet-stream\r\n");
87
88             // add basic authentication header
89
byte[] encodedPassword = (user + ":" + password).getBytes();
90             BASE64Encoder encoder = new BASE64Encoder();
91             wr.write("Authorization: Basic " + encoder.encode(encodedPassword) + "\r\n");
92             wr.write("\r\n");
93             wr.flush();
94
95             // write upload file data
96
byte[] buffer = new byte[1024*64];
97             for (int i = bis.read(buffer); i > 0; i = bis.read(buffer)) {
98                 out.write(buffer, 0, i);
99             }
100             out.flush();
101
102             // read HTTP response
103
BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
104             Vector v = new Vector();
105             for (int timeoutCounter = 0; timeoutCounter < 120; timeoutCounter++) {
106                 boolean finished = false;
107                 while (reader.ready()) {
108                     String JavaDoc curLine = reader.readLine();
109                     v.add(curLine);
110                     if (curLine.startsWith("SUCCESS:")) {
111                         finished = true;
112                     }
113                 }
114                 if (finished) {
115                     break;
116                 } else {
117                     Thread.sleep(500);
118                 }
119             }
120
121             if (v.size() == 0) {
122                 throw new Exception JavaDoc("Upload file failed: no server response received");
123             }
124             
125             // parse HTTP response strings
126
boolean isData = false;
127             int i = 0;
128             String JavaDoc responseString = null;
129             for (Enumeration e = v.elements(); e.hasMoreElements(); i++) {
130                 String JavaDoc curElement = (String JavaDoc) e.nextElement();
131                 
132                 // check response code
133
if (i == 0) {
134                     String JavaDoc responseCode = curElement.substring(curElement.indexOf(" ") + 1);
135                     if (!responseCode.startsWith("200")) {
136                         throw new Exception JavaDoc("HTTP connection failed: " + responseCode);
137                     }
138                 }
139                 // check start of data
140
if (curElement.equals("")) {
141                     isData = true;
142                     continue;
143                 }
144                 // get first line of data
145
if (isData) {
146                     responseString = curElement;
147                     break;
148                 }
149             }
150
151             if (responseString == null) {
152                 throw new Exception JavaDoc("Upload file failed: no server response received");
153             }
154
155             if (!responseString.startsWith("SUCCESS:")) {
156                 throw new Exception JavaDoc("Upload file failed: " + responseString);
157             }
158
159             // parse response string to get remote file path
160
String JavaDoc remotePath = responseString.substring(8);
161
162             return remotePath;
163                 
164         } catch (Exception JavaDoc e) {
165             throw e;
166         } finally {
167             if (socket != null) {
168                 socket.close();
169             }
170             if (bis != null) {
171                 bis.close();
172             }
173         }
174             
175     }
176             
177 }
178
Popular Tags