1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.module.admininterface.pages;
35
36 import info.magnolia.cms.beans.runtime.Document;
37 import info.magnolia.cms.security.AccessDeniedException;
38 import info.magnolia.cms.security.Permission;
39 import info.magnolia.cms.util.AlertUtil;
40 import info.magnolia.importexport.DataTransporter;
41 import info.magnolia.repository.RepositoryConstants;
42
43 import java.text.MessageFormat;
44
45 import javax.servlet.ServletException;
46 import javax.servlet.http.HttpServletRequest;
47 import javax.servlet.http.HttpServletResponse;
48
49 import org.apache.commons.lang.StringEscapeUtils;
50 import org.apache.commons.lang.StringUtils;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53
54
55
56
57
58
59
60 public class ImportPage extends ExportPage {
61
62
63
64
65 private static final long serialVersionUID = 222L;
66
67
68
69
70 private static Logger log = LoggerFactory.getLogger(ImportPage.class);
71
72 private Document mgnlFileImport;
73
74 private String mgnlRedirect;
75
76 private int mgnlUuidBehavior;
77
78
79
80
81
82
83 public ImportPage(String name, HttpServletRequest request, HttpServletResponse response) {
84 super(name, request, response);
85 }
86
87
88
89
90 @Override
91 public String getCommand() {
92 if ("POST".equals(getRequest().getMethod())) {
93 return "importxml";
94 }
95 return super.getCommand();
96 }
97
98
99
100
101
102 public Document getMgnlFileImport() {
103 return this.mgnlFileImport;
104 }
105
106
107
108
109
110 public void setMgnlFileImport(Document mgnlFileImport) {
111 this.mgnlFileImport = mgnlFileImport;
112 }
113
114
115
116
117
118 public String getMgnlRedirect() {
119 return this.mgnlRedirect;
120 }
121
122
123
124
125
126 public void setMgnlRedirect(String mgnlRedirect) {
127 this.mgnlRedirect = mgnlRedirect;
128 }
129
130
131
132
133
134 public int getMgnlUuidBehavior() {
135 return this.mgnlUuidBehavior;
136 }
137
138
139
140
141
142 public void setMgnlUuidBehavior(int mgnlUuidBehavior) {
143 this.mgnlUuidBehavior = mgnlUuidBehavior;
144 }
145
146
147
148
149 public String importxml() throws Exception {
150
151 if (mgnlFileImport == null) {
152 AlertUtil.setMessage("Select a file to import.");
153 throw new ServletException("Select a file to import.");
154 }
155
156 if (log.isDebugEnabled()) {
157 log.debug("Import request received.");
158 }
159
160 if (StringUtils.isEmpty(mgnlRepository)) {
161 mgnlRepository = RepositoryConstants.WEBSITE;
162 }
163 if (StringUtils.isEmpty(mgnlPath)) {
164 mgnlPath = "/";
165 }
166
167 checkPath();
168
169 if (!checkPermissions(request, mgnlRepository, mgnlPath, Permission.WRITE)) {
170
171
172 AlertUtil.setMessage("Write permission needed for import. User not allowed to WRITE path [" + StringEscapeUtils.escapeHtml(mgnlPath) + "]");
173
174 throw new ServletException(new AccessDeniedException(
175 "Write permission needed for import. User not allowed to WRITE path ["
176 + StringEscapeUtils.escapeHtml(mgnlPath)
177 + "]"));
178 }
179 try{
180 DataTransporter.importDocument(
181 mgnlFileImport,
182 mgnlRepository,
183 mgnlPath,
184 false,
185 mgnlUuidBehavior,
186 true,
187 true);
188 } finally {
189 mgnlFileImport.getFile().delete();
190 }
191
192 log.info("Import done");
193
194 if (StringUtils.isNotBlank(mgnlRedirect)) {
195 if (log.isInfoEnabled()) {
196 log.info(MessageFormat.format("Redirecting to [{0}]",
197 new Object[]{mgnlRedirect}));
198 }
199 response.sendRedirect(mgnlRedirect);
200 return null;
201 }
202 return this.show();
203 }
204 }