DefaultFileTransferManager.java 8.27 KB
Newer Older
1 2 3 4 5
/**
 * $RCSfile$
 * $Revision: 1217 $
 * $Date: 2005-04-11 18:11:06 -0300 (Mon, 11 Apr 2005) $
 *
6
 * Copyright (C) 1999-2008 Jive Software. All rights reserved.
7
 *
8 9 10 11 12 13 14 15 16 17 18
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
19
 */
20
package org.jivesoftware.openfire.filetransfer;
21

22
import org.dom4j.Element;
23 24 25 26 27 28 29 30
import org.jivesoftware.openfire.auth.UnauthorizedException;
import org.jivesoftware.openfire.container.BasicModule;
import org.jivesoftware.openfire.filetransfer.proxy.ProxyConnectionManager;
import org.jivesoftware.openfire.filetransfer.proxy.ProxyTransfer;
import org.jivesoftware.openfire.interceptor.InterceptorManager;
import org.jivesoftware.openfire.interceptor.PacketInterceptor;
import org.jivesoftware.openfire.interceptor.PacketRejectedException;
import org.jivesoftware.openfire.session.Session;
31 32 33
import org.jivesoftware.util.JiveGlobals;
import org.jivesoftware.util.cache.Cache;
import org.jivesoftware.util.cache.CacheFactory;
34 35
import org.xmpp.packet.IQ;
import org.xmpp.packet.JID;
36
import org.xmpp.packet.Packet;
37

38
import java.util.ArrayList;
39
import java.util.List;
40 41 42 43 44 45

/**
 * Provides several utility methods for file transfer manager implementaions to utilize.
 *
 * @author Alexander Wenckus
 */
46
public class DefaultFileTransferManager extends BasicModule implements FileTransferManager {
47

48 49
    private static final String CACHE_NAME = "File Transfer Cache";

50
    private final Cache<String, FileTransfer> fileTransferMap;
51

52 53 54
    private final List<FileTransferInterceptor> fileTransferInterceptorList
            = new ArrayList<FileTransferInterceptor>();

55 56 57
    /**
     * Default constructor creates the cache.
     */
58
    public DefaultFileTransferManager() {
59
        super("File Transfer Manager");
60
        fileTransferMap = CacheFactory.createCache(CACHE_NAME);
61
        InterceptorManager.getInstance().addInterceptor(new MetaFileTransferInterceptor());
62 63
    }

64
    /**
65 66
     * Returns true if the proxy transfer should be matched to an existing file transfer
     * in the system.
67
     *
68 69
     * @return Returns true if the proxy transfer should be matched to an existing file
     * transfer in the system.
70 71
     */
    public boolean isMatchProxyTransfer() {
72
        return JiveGlobals.getBooleanProperty("xmpp.proxy.transfer.required", true);
73 74
    }

75
    protected void cacheFileTransfer(String key, FileTransfer transfer) {
76 77 78
        fileTransferMap.put(key, transfer);
    }

79
    protected FileTransfer retrieveFileTransfer(String key) {
80 81 82
        return fileTransferMap.get(key);
    }

83
    protected static Element getChildElement(Element element, String namespace) {
84 85
        //noinspection unchecked
        List<Element> elements = element.elements();
86 87 88
        if (elements.isEmpty()) {
            return null;
        }
89
        for (Element childElement : elements) {
90 91 92 93 94 95 96 97 98
            String childNamespace = childElement.getNamespaceURI();
            if (namespace.equals(childNamespace)) {
                return childElement;
            }
        }

        return null;
    }

99
    public boolean acceptIncomingFileTransferRequest(FileTransfer transfer)
100
            throws FileTransferRejectedException
101
    {
102
        fireFileTransferIntercept(transfer, false);
103 104 105 106 107 108 109 110 111 112
        if(transfer != null) {
            String streamID = transfer.getSessionID();
            JID from = new JID(transfer.getInitiator());
            JID to = new JID(transfer.getTarget());
            cacheFileTransfer(ProxyConnectionManager.createDigest(streamID, from, to), transfer);
            return true;
        }
        return false;
    }

113 114 115 116 117 118 119 120 121 122 123 124 125 126
    public void registerProxyTransfer(String transferDigest, ProxyTransfer proxyTransfer)
            throws UnauthorizedException
    {
        FileTransfer transfer = retrieveFileTransfer(transferDigest);
        if (isMatchProxyTransfer() && transfer == null) {
            throw new UnauthorizedException("Unable to match proxy transfer with a file transfer");
        }
        else if (transfer == null) {
            return;
        }

        transfer.setProgress(proxyTransfer);
        cacheFileTransfer(transferDigest, transfer);
    }
127

128 129
    private FileTransfer createFileTransfer(JID from,
                                            JID to, Element siElement) {
130 131 132
        String streamID = siElement.attributeValue("id");
        String mimeType = siElement.attributeValue("mime-type");
        // Check profile, the only type we deal with currently is file transfer
133 134 135 136 137 138 139 140 141 142
        Element fileTransferElement = getChildElement(siElement, NAMESPACE_SI_FILETRANSFER);
        // Not valid form, reject
        if (fileTransferElement == null) {
            return null;
        }
        String fileName = fileTransferElement.attributeValue("name");
        String sizeString = fileTransferElement.attributeValue("size");
        if (fileName == null || sizeString == null) {
            return null;
        }
143

144 145 146
        long size;
        try {
            size = Long.parseLong(sizeString);
147
        }
148 149 150 151 152
        catch (Exception ex) {
            return null;
        }

        return new FileTransfer(from.toString(), to.toString(), streamID, fileName, size, mimeType);
153 154
    }

155
    public void addFileTransferInterceptor(FileTransferInterceptor interceptor) {
156
        fileTransferInterceptorList.add(interceptor);
157 158 159
    }

    public void removeFileTransferInterceptor(FileTransferInterceptor interceptor) {
160
        fileTransferInterceptorList.remove(interceptor);
161 162
    }

163
    public void fireFileTransferIntercept(FileTransferProgress transfer, boolean isReady)
164 165
            throws FileTransferRejectedException
    {
166
        fireFileTransferIntercept(fileTransferMap.get(transfer.getSessionID()), isReady);
167 168
    }

169 170
    private void fireFileTransferIntercept(FileTransfer transfer, boolean isReady)
            throws FileTransferRejectedException
171
    {
172 173 174
        for(FileTransferInterceptor interceptor : fileTransferInterceptorList) {
            interceptor.interceptFileTransfer(transfer, isReady);
        }
175 176
    }

177 178 179
    /**
     * Interceptor to grab and validate file transfer meta information.
     */
180
    private class MetaFileTransferInterceptor implements PacketInterceptor {
181 182 183 184 185 186 187 188 189 190 191 192 193
        public void interceptPacket(Packet packet, Session session, boolean incoming,
                                    boolean processed)
                throws PacketRejectedException
        {
            // We only want packets recieved by the server
            if (!processed && incoming && packet instanceof IQ) {
                IQ iq = (IQ) packet;
                Element childElement = iq.getChildElement();
                if(childElement == null) {
                    return;
                }

                String namespace = childElement.getNamespaceURI();
194 195 196
                String profile = childElement.attributeValue("profile");
                // Check that the SI is about file transfer and try creating a file transfer
                if (NAMESPACE_SI.equals(namespace) && NAMESPACE_SI_FILETRANSFER.equals(profile)) {
197 198 199 200
                    // If this is a set, check the feature offer
                    if (iq.getType().equals(IQ.Type.set)) {
                        JID from = iq.getFrom();
                        JID to = iq.getTo();
201

202
                        FileTransfer transfer = createFileTransfer(from, to, childElement);
203

204 205 206 207 208 209 210
                        try {
                            if (transfer == null || !acceptIncomingFileTransferRequest(transfer)) {
                                throw new PacketRejectedException();
                            }
                        }
                        catch (FileTransferRejectedException e) {
                            throw new PacketRejectedException(e);
211 212 213 214 215 216
                        }
                    }
                }
            }
        }
    }
217
}