View Javadoc

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