View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 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.admininterface.pages;
35  
36  import info.magnolia.cms.beans.config.ContentRepository;
37  import info.magnolia.cms.beans.runtime.Document;
38  import info.magnolia.cms.security.AccessDeniedException;
39  import info.magnolia.cms.security.Permission;
40  import info.magnolia.importexport.DataTransporter;
41  import org.apache.commons.lang.StringUtils;
42  import org.slf4j.Logger;
43  import org.slf4j.LoggerFactory;
44  
45  import javax.servlet.ServletException;
46  import javax.servlet.http.HttpServletRequest;
47  import javax.servlet.http.HttpServletResponse;
48  import java.text.MessageFormat;
49  
50  
51  /**
52   * @author Fabrizio Giustina
53   * @version $Id: ImportPage.java 32667 2010-03-13 00:37:06Z gjoseph $
54   */
55  public class ImportPage extends ExportPage {
56  
57      /**
58       * Stable serialVersionUID.
59       */
60      private static final long serialVersionUID = 222L;
61  
62      /**
63       * Logger.
64       */
65      private static Logger log = LoggerFactory.getLogger(ImportPage.class);
66  
67      private Document mgnlFileImport;
68  
69      private String mgnlRedirect;
70  
71      private int mgnlUuidBehavior;
72  
73      /**
74       * @param name
75       * @param request
76       * @param response
77       */
78      public ImportPage(String name, HttpServletRequest request, HttpServletResponse response) {
79          super(name, request, response);
80      }
81  
82      /**
83       * @see info.magnolia.module.admininterface.pages.ExportPage#getCommand()
84       */
85      public String getCommand() {
86          if ("POST".equals(getRequest().getMethod())) {
87              return "importxml"; // a post request is an import request, preserve the behaviour from the servlet
88          }
89          return super.getCommand();
90      }
91  
92      /**
93       * Getter for <code>mgnlFileImport</code>.
94       * @return Returns the mgnlFileImport.
95       */
96      public Document getMgnlFileImport() {
97          return this.mgnlFileImport;
98      }
99  
100     /**
101      * Setter for <code>mgnlFileImport</code>.
102      * @param mgnlFileImport The mgnlFileImport to set.
103      */
104     public void setMgnlFileImport(Document mgnlFileImport) {
105         this.mgnlFileImport = mgnlFileImport;
106     }
107 
108     /**
109      * Getter for <code>mgnlRedirect</code>.
110      * @return Returns the mgnlRedirect.
111      */
112     public String getMgnlRedirect() {
113         return this.mgnlRedirect;
114     }
115 
116     /**
117      * Setter for <code>mgnlRedirect</code>.
118      * @param mgnlRedirect The mgnlRedirect to set.
119      */
120     public void setMgnlRedirect(String mgnlRedirect) {
121         this.mgnlRedirect = mgnlRedirect;
122     }
123 
124     /**
125      * Getter for <code>mgnlUuidBehavior</code>.
126      * @return Returns the mgnlUuidBehavior.
127      */
128     public int getMgnlUuidBehavior() {
129         return this.mgnlUuidBehavior;
130     }
131 
132     /**
133      * Setter for <code>mgnlUuidBehavior</code>.
134      * @param mgnlUuidBehavior The mgnlUuidBehavior to set.
135      */
136     public void setMgnlUuidBehavior(int mgnlUuidBehavior) {
137         this.mgnlUuidBehavior = mgnlUuidBehavior;
138     }
139 
140     /**
141      * @throws Exception
142      */
143     public String importxml() throws Exception {
144 
145         if (log.isDebugEnabled()) {
146             log.debug("Import request received."); //$NON-NLS-1$
147         }
148 
149         if (StringUtils.isEmpty(mgnlRepository)) {
150             mgnlRepository = ContentRepository.WEBSITE;
151         }
152         if (StringUtils.isEmpty(mgnlPath)) {
153             mgnlPath = "/"; //$NON-NLS-1$
154         }
155 
156         if (!checkPermissions(request, mgnlRepository, mgnlPath, Permission.WRITE)) {
157             throw new ServletException(new AccessDeniedException(
158                 "Write permission needed for import. User not allowed to WRITE path [" //$NON-NLS-1$
159                     + mgnlPath
160                     + "]")); //$NON-NLS-1$
161         }
162         
163         DataTransporter.importDocument(
164             mgnlFileImport,
165             mgnlRepository,
166             mgnlPath,
167             mgnlKeepVersions,
168             mgnlUuidBehavior,
169             true,
170             true);
171 
172         log.info("Import done"); //$NON-NLS-1$
173 
174         if (StringUtils.isNotBlank(mgnlRedirect)) {
175             if (log.isInfoEnabled()) {
176                 log.info(MessageFormat.format("Redirecting to [{0}]", //$NON-NLS-1$
177                     new Object[]{mgnlRedirect}));
178             }
179             response.sendRedirect(mgnlRedirect);
180             return null;
181         }
182         return this.show();
183     }
184 
185 }