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