1 /**
2 *
3 * Copyright 2003-2004 The Apache Software Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.geronimo.deployment.remote;
18
19 import javax.servlet.http.HttpServlet;
20 import javax.servlet.http.HttpServletRequest;
21 import javax.servlet.http.HttpServletResponse;
22 import javax.servlet.ServletException;
23 import java.io.DataOutputStream;
24 import java.io.DataInputStream;
25 import java.io.File;
26 import java.io.IOException;
27 import java.io.BufferedOutputStream;
28 import java.io.FileOutputStream;
29
30 /**
31 * A servlet that accepts file uploads. It takes only POST requests, which should
32 * contain a Java "DataOutput" formatted stream containing:
33 *
34 * 1) an int, the number of files being uploaded
35 * 2) for each file:
36 * 1) an int, the length of the file in bytes
37 * 2) a number of raw bytes equal to the above for the file
38 *
39 * It returns a serialized stream containing:
40 *
41 * 1) a UTF string, the status (should be "OK")
42 * 2) an int, the number of files received
43 * 3) for each file:
44 * 1) a UTF String, the path to the file as saved to the server's filesystem
45 *
46 * The file positions in the response will be the same as in the request.
47 * The is, a name for upload file #2 will be in response position #2.
48 *
49 * @version $Rev: 356022 $ $Date: 2005-12-11 12:58:34 -0800 (Sun, 11 Dec 2005) $
50 */
51 public class FileUploadServlet extends HttpServlet {
52 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
53 int fileCount;
54 String names[];
55 try {
56 DataInputStream in = new DataInputStream(request.getInputStream());
57 fileCount = in.readInt();
58 names = new String[fileCount];
59 for(int i=0; i<fileCount; i++) {
60 int length = in.readInt();
61 File temp = File.createTempFile("remote-deploy", "");
62 temp.deleteOnExit();
63 names[i] = temp.getAbsolutePath();
64 readToFile(in, temp, length);
65 }
66 in.close();
67 } catch (IOException e) {
68 DataOutputStream out = new DataOutputStream(response.getOutputStream());
69 out.writeUTF("ERROR: "+e.getMessage());
70 out.close();
71 return;
72 }
73 DataOutputStream out = new DataOutputStream(response.getOutputStream());
74 out.writeUTF("OK");
75 out.writeInt(fileCount);
76 for (int i = 0; i < names.length; i++) {
77 out.writeUTF(names[i]);
78 }
79 out.flush();
80 out.close();
81 }
82
83 private static void readToFile(DataInputStream in, File temp, int length) throws IOException {
84 BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(temp));
85 int total, read;
86 try {
87 byte[] buf = new byte[1024];
88 total = 0;
89 while((read = in.read(buf, 0, Math.min(buf.length, length - total))) > -1) {
90 out.write(buf, 0, read);
91 total += read;
92 if(total == length) {
93 break;
94 }
95 }
96 } finally {
97 try {out.flush();} catch (IOException e) {}
98 out.close();
99 }
100 if(total != length) {
101 throw new IOException("Unable to read entire upload file ("+total+"b expecting "+length+"b)");
102 }
103 }
104 }
105