-
Notifications
You must be signed in to change notification settings - Fork 594
/
CreateCloudConfigServlet.java
104 lines (88 loc) · 3.84 KB
/
CreateCloudConfigServlet.java
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* #%L
* ACS AEM Commons Bundle
* %%
* Copyright (C) 2019 Adobe
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package com.adobe.acs.commons.cloudconfig.impl;
import java.io.IOException;
import java.util.Collections;
import javax.annotation.Nonnull;
import javax.servlet.Servlet;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.servlets.post.HtmlResponse;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.adobe.aemds.guide.utils.JcrResourceConstants;
import com.day.cq.commons.jcr.JcrConstants;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.WCMException;
@Component(service = Servlet.class, property = { "sling.servlet.methods=" + HttpConstants.METHOD_POST,
"sling.servlet.resourceTypes=acs-commons/components/utilities/cloudconfig/cloudconfiglist" })
public class CreateCloudConfigServlet extends SlingAllMethodsServlet {
private static final long serialVersionUID = -397622433323474345L;
private static final Logger log = LoggerFactory.getLogger(CreateCloudConfigServlet.class);
@Override
protected void doPost(@Nonnull SlingHttpServletRequest request, @Nonnull SlingHttpServletResponse response)
throws IOException {
ResourceResolver resolver = request.getResourceResolver();
PageManager pageManager = resolver.adaptTo(PageManager.class);
HtmlResponse resp = new HtmlResponse();
if (pageManager == null) {
resp.setError(new IOException("Unable to get page manager"));
} else {
String configPath = getParam(request, "configPath");
Resource cloudConfigFolder = ResourceUtil.getOrCreateResource(resolver, configPath + "/settings/cloudconfigs",
Collections.singletonMap(JcrConstants.JCR_PRIMARYTYPE, JcrResourceConstants.NT_SLING_FOLDER),
JcrResourceConstants.NT_SLING_FOLDER, false);
log.debug("Creating Cloud Config in: {}", cloudConfigFolder);
resp.setParentLocation(cloudConfigFolder.getPath());
// create a new page
Page page;
try {
page = pageManager.create(cloudConfigFolder.getPath(), getParam(request, "name"), getParam(request, "template"),
getParam(request, "title"));
resp.setPath(page.getPath());
resp.setLocation(page.getPath());
resp.setStatus(200, "Created Cloud Configuration");
log.debug("Created configuration: {}", page.getPath());
resolver.commit();
} catch (WCMException e) {
resp.setError(e);
}
}
response.setContentType("text/plain");
resp.send(response, true);
}
private String getParam(SlingHttpServletRequest request, String param) throws IOException {
String value = request.getParameter(param);
if (StringUtils.isBlank(value)) {
throw new IOException("Parameter " + param + " must not be blank");
} else {
log.debug("Loaded {} for parameter {}", value, param);
}
return value;
}
}