001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  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: 547260 $ $Date: 2007-06-14 10:42:29 -0400 (Thu, 14 Jun 2007) $
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                        in.close();
082                    }
083                    out.flush();
084                    out.close();
085                    DataInputStream in = new DataInputStream(new BufferedInputStream(con.getInputStream()));
086                    String status = in.readUTF();
087                    if(!status.equals("OK")) {
088                        progress.fail("Unable to upload files to server: "+status);
089                        return;
090                    }
091                    progress.updateStatus("File upload complete (Server: "+status+")");
092                    int count = in.readInt();
093                    if(count != valid.size()) {
094                        progress.fail("Server did not receive all "+valid.size()+" files ("+count+")");
095                    }
096                    for (Iterator it = valid.iterator(); it.hasNext();) {
097                        Integer index = (Integer) it.next();
098                        String serverFileName = in.readUTF();
099                        files[index.intValue()] = new File(serverFileName);
100                    }
101                    in.close();
102                    progress.updateStatus(count+" file(s) transferred to server.  Resuming deployment operation.");
103                } catch (Exception e) {
104                    progress.doFail(e);
105                }
106            }
107        }
108    
109        private static URLConnection connectToServer(URL url, String username, String password) throws IOException {
110            URLConnection con = url.openConnection();
111            String auth = username + ":" + password;
112            byte[] data = auth.getBytes();
113            String s = new String(Base64.encode(data));
114            while(s.length() % 4 != 0) s += "=";
115            con.setRequestProperty("Authorization", "Basic "+s);
116            con.setDoInput(true);
117            con.setDoOutput(true);
118            con.connect();
119            return con;
120        }
121    }