View Javadoc

1   /**
2    * This file Copyright (c) 2003-2014 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module.exchangesimple;
35  
36  import info.magnolia.cms.exchange.ActivationManagerFactory;
37  import info.magnolia.cms.exchange.ExchangeException;
38  import info.magnolia.cms.exchange.Subscriber;
39  import info.magnolia.module.exchangesimple.monitor.ActivationMonitor;
40  import info.magnolia.objectfactory.Components;
41  
42  import java.io.File;
43  import java.io.FileInputStream;
44  import java.io.OutputStream;
45  import java.net.HttpURLConnection;
46  import java.util.Collection;
47  
48  import org.apache.commons.io.IOUtils;
49  import org.apache.commons.lang.StringUtils;
50  import org.slf4j.Logger;
51  import org.slf4j.LoggerFactory;
52  
53  /**
54   * Class responsible for transport of activated content.
55   * 
56   * @author Sameer Charles $Id$
57   */
58  public class Transporter {
59  
60      private static Logger log = LoggerFactory.getLogger(Transporter.class);
61  
62      /**
63       * Content boundary in the multipart request.
64       */
65      public static final String BOUNDARY = "mgnlExchange-cfc93688d385";
66  
67      /**
68       * Initiates multipart http post to transport content to the subscriber.
69       * 
70       * @param connection
71       * @param activationContent
72       * @throws ExchangeException
73       */
74      public static void transport(HttpURLConnection connection, ActivationContent activationContent)
75              throws ExchangeException {
76          File tempFile = null;
77          FileInputStream is = null;
78          OutputStream os = null;
79  
80          try {
81              tempFile = activationContent.getTempFile();
82  
83              // The only purpose of the method below is to disable buffering. This makes sense since we use IOUtils.copy to copy the content to the output stream, however method is not available in Java 1.4 and have to be commented out as long as we support it.
84              // connection.setFixedLengthStreamingMode((int) tempFile.length());
85              connection.setDoOutput(true);
86              connection.setDoInput(true);
87              connection.setUseCaches(false);
88              connection.setRequestProperty("Content-type", "multipart/form-data; boundary=" + BOUNDARY);
89              connection.setRequestProperty("Cache-Control", "no-cache");
90  
91              is = new FileInputStream(tempFile);
92              if (tempFile.length() > Integer.MAX_VALUE) {
93                  connection.setChunkedStreamingMode(4096);
94              }
95              else {
96                  connection.setFixedLengthStreamingMode((int) tempFile.length());
97              }
98  
99              long start = System.currentTimeMillis();
100             os = connection.getOutputStream();
101             long end = System.currentTimeMillis();
102 
103             String subscriberName = null;
104             Collection<Subscriber> subscribers = ActivationManagerFactory.getActivationManager().getSubscribers();
105             for (Subscriber subscriber : subscribers) {
106                 if (subscriber.getURL().equals(StringUtils.substringBefore(connection.getURL().toString(), "/" + BaseSyndicatorImpl.DEFAULT_HANDLER))) {
107                     subscriberName = subscriber.getName();
108                 }
109             }
110             ActivationMonitor activationMonitor = Components.getComponent(ActivationMonitor.class);
111             activationMonitor.setSubscriberResponseTime(subscriberName, end - start);
112 
113             IOUtils.copy(is, os);
114         } catch (Exception e) {
115             String msg = "Simple exchange transport failed: " + e.getMessage();
116             log.error(msg, e);
117             throw new ExchangeException(msg, e);
118         } finally {
119             IOUtils.closeQuietly(is);
120             IOUtils.closeQuietly(os);
121         }
122     }
123 }