Home » activemq-parent-5.3.1-source-release » org.apache » activemq » blob » [javadoc | source]

    1   /**
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  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.activemq.blob;
   18   
   19   import java.io.File;
   20   import java.io.FileInputStream;
   21   import java.io.IOException;
   22   import java.io.InputStream;
   23   import java.io.OutputStream;
   24   import java.net.HttpURLConnection;
   25   import java.net.MalformedURLException;
   26   import java.net.URL;
   27   
   28   import javax.jms.JMSException;
   29   
   30   import org.apache.activemq.command.ActiveMQBlobMessage;
   31   
   32   /**
   33    * A default implementation of {@link BlobUploadStrategy} which uses the URL
   34    * class to upload files or streams to a remote URL
   35    */
   36   public class DefaultBlobUploadStrategy implements BlobUploadStrategy {
   37       private BlobTransferPolicy transferPolicy;
   38   
   39       public DefaultBlobUploadStrategy(BlobTransferPolicy transferPolicy) {
   40           this.transferPolicy = transferPolicy;
   41       }
   42   
   43       public URL uploadFile(ActiveMQBlobMessage message, File file) throws JMSException, IOException {
   44           return uploadStream(message, new FileInputStream(file));
   45       }
   46   
   47       public URL uploadStream(ActiveMQBlobMessage message, InputStream fis) throws JMSException, IOException {
   48           URL url = createUploadURL(message);
   49   
   50           HttpURLConnection connection = (HttpURLConnection)url.openConnection();
   51           connection.setRequestMethod("PUT");
   52           connection.setDoOutput(true);
   53   
   54           // use chunked mode or otherwise URLConnection loads everything into
   55           // memory
   56           // (chunked mode not supported before JRE 1.5)
   57           connection.setChunkedStreamingMode(transferPolicy.getBufferSize());
   58   
   59           OutputStream os = connection.getOutputStream();
   60   
   61           byte[] buf = new byte[transferPolicy.getBufferSize()];
   62           for (int c = fis.read(buf); c != -1; c = fis.read(buf)) {
   63               os.write(buf, 0, c);
   64               os.flush();
   65           }
   66           os.close();
   67           fis.close();
   68   
   69           if (!isSuccessfulCode(connection.getResponseCode())) {
   70               throw new IOException("PUT was not successful: " + connection.getResponseCode() + " "
   71                                     + connection.getResponseMessage());
   72           }
   73   
   74           return url;
   75       }
   76   
   77       public void deleteFile(ActiveMQBlobMessage message) throws IOException, JMSException {
   78           URL url = createUploadURL(message);
   79   
   80           HttpURLConnection connection = (HttpURLConnection)url.openConnection();
   81           connection.setRequestMethod("DELETE");
   82           connection.connect();
   83           connection.disconnect();
   84   
   85           if (!isSuccessfulCode(connection.getResponseCode())) {
   86               throw new IOException("DELETE was not successful: " + connection.getResponseCode() + " "
   87                                     + connection.getResponseMessage());
   88           }
   89       }
   90   
   91       private boolean isSuccessfulCode(int responseCode) {
   92           return responseCode >= 200 && responseCode < 300; // 2xx => successful
   93       }
   94   
   95       protected URL createUploadURL(ActiveMQBlobMessage message) throws JMSException, MalformedURLException {
   96           return new URL(transferPolicy.getUploadUrl() + message.getMessageId().toString());
   97       }
   98   }

Home » activemq-parent-5.3.1-source-release » org.apache » activemq » blob » [javadoc | source]