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