001 /**
002 *
003 * Copyright 2003-2004 The Apache Software Foundation
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.geronimo.deployment.plugin.remote;
018
019 import org.apache.geronimo.deployment.plugin.local.AbstractDeployCommand;
020 import org.apache.geronimo.util.encoders.Base64;
021
022 import java.io.File;
023 import java.io.IOException;
024 import java.io.DataOutputStream;
025 import java.io.BufferedInputStream;
026 import java.io.FileInputStream;
027 import java.io.BufferedOutputStream;
028 import java.io.DataInputStream;
029 import java.net.URL;
030 import java.net.URLConnection;
031 import java.util.LinkedList;
032 import java.util.List;
033 import java.util.Iterator;
034
035 /**
036 * Knows how to upload files to a server
037 *
038 * @version $Rev: 355877 $ $Date: 2005-12-10 18:48:27 -0800 (Sat, 10 Dec 2005) $
039 */
040 public class RemoteDeployUtil {
041 public static void uploadFilesToServer(File[] files, AbstractDeployCommand progress) {
042 if(files == null) {
043 return;
044 }
045 List valid = new LinkedList();
046 for(int i=0; i<files.length; i++) {
047 if(files[i] == null) {
048 continue;
049 }
050 File file = files[i];
051 if(!file.exists() || !file.canRead()) {
052 continue;
053 }
054 valid.add(new Integer(i));
055 }
056 if(valid.size() > 0) {
057 progress.updateStatus("Uploading "+valid.size()+" file(s) to server");
058 try {
059 URL url = progress.getRemoteDeployUploadURL();
060 URLConnection con = connectToServer(url, progress.getCommandContext().getUsername(), progress.getCommandContext().getPassword());
061 DataOutputStream out = new DataOutputStream(new BufferedOutputStream(con.getOutputStream()));
062 out.writeInt(valid.size());
063 byte[] buf = new byte[1024];
064 int size, total, length, threshold, next;
065 for (Iterator it = valid.iterator(); it.hasNext();) {
066 Integer index = (Integer) it.next();
067 File file = files[index.intValue()];
068 out.writeInt(length = (int)file.length());
069 threshold = Math.max(length / 100, 10240);
070 next = threshold;
071 BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
072 total = 0;
073 while((size = in.read(buf)) > -1) {
074 out.write(buf, 0, size);
075 total += size;
076 if(total > next) {
077 progress.updateStatus("Uploading "+file.getName()+": "+(total/1024)+" kB");
078 while(total > next) next += threshold;
079 }
080 }
081 }
082 out.flush();
083 out.close();
084 DataInputStream in = new DataInputStream(new BufferedInputStream(con.getInputStream()));
085 String status = in.readUTF();
086 if(!status.equals("OK")) {
087 progress.fail("Unable to upload files to server: "+status);
088 return;
089 }
090 progress.updateStatus("File upload complete (Server: "+status+")");
091 int count = in.readInt();
092 if(count != valid.size()) {
093 progress.fail("Server did not receive all "+valid.size()+" files ("+count+")");
094 }
095 for (Iterator it = valid.iterator(); it.hasNext();) {
096 Integer index = (Integer) it.next();
097 String serverFileName = in.readUTF();
098 files[index.intValue()] = new File(serverFileName);
099 }
100 in.close();
101 progress.updateStatus(count+" file(s) transferred to server. Resuming deployment operation.");
102 } catch (Exception e) {
103 progress.doFail(e);
104 }
105 }
106 }
107
108 private static URLConnection connectToServer(URL url, String username, String password) throws IOException {
109 URLConnection con = url.openConnection();
110 String auth = username + ":" + password;
111 byte[] data = auth.getBytes();
112 String s = new String(Base64.encode(data));
113 while(s.length() % 4 != 0) s += "=";
114 con.setRequestProperty("Authorization", "Basic "+s);
115 con.setDoInput(true);
116 con.setDoOutput(true);
117 con.connect();
118 return con;
119 }
120 }