1 /*******************************************************************************
2 * ============LICENSE_START==================================================
4 * * ===========================================================================
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * * ===========================================================================
7 * * Licensed under the Apache License, Version 2.0 (the "License");
8 * * you may not use this file except in compliance with the License.
9 * * You may obtain a copy of the License at
11 * * http://www.apache.org/licenses/LICENSE-2.0
13 * * Unless required by applicable law or agreed to in writing, software
14 * * distributed under the License is distributed on an "AS IS" BASIS,
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * * See the License for the specific language governing permissions and
17 * * limitations under the License.
18 * * ============LICENSE_END====================================================
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 ******************************************************************************/
23 package org.onap.dmaap.datarouter.subscriber;
25 import org.apache.commons.codec.binary.Base64;
26 import org.apache.log4j.Logger;
28 import javax.servlet.http.HttpServlet;
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
32 import java.net.URLEncoder;
33 import java.nio.file.Files;
34 import java.nio.file.Paths;
35 import java.nio.file.StandardCopyOption;
37 public class SampleSubscriberServlet extends HttpServlet {
39 private static Logger logger =
40 Logger.getLogger("org.onap.dmaap.datarouter.subscriber.SampleSubscriberServlet");
41 private static String outputDirectory;
42 private static String basicAuth;
45 * Configure the SampleSubscriberServlet.
48 * <li>Login - The login expected in the Authorization header (default "LOGIN").
49 * <li>Password - The password expected in the Authorization header (default "PASSWORD").
50 * <li>outputDirectory - The directory where files are placed (default
51 * "/opt/app/subscriber/delivery").
56 SubscriberProps props = SubscriberProps.getInstance();
57 String login = props.getValue("org.onap.dmaap.datarouter.subscriber.auth.user", "LOGIN");
59 props.getValue("org.onap.dmaap.datarouter.subscriber.auth.password", "PASSWORD");
62 "org.onap.dmaap.datarouter.subscriber.delivery.dir", "/opt/app/subscriber/delivery");
64 Files.createDirectory(Paths.get(outputDirectory));
65 } catch (IOException e) {
66 logger.info("SubServlet: Failed to create delivery dir: " + e.getMessage());
68 basicAuth = "Basic " + Base64.encodeBase64String((login + ":" + password).getBytes());
72 protected void doPut(HttpServletRequest req, HttpServletResponse resp) {
74 common(req, resp, false);
75 } catch (IOException e) {
77 "SampleSubServlet: Failed to doPut: " + req.getRemoteAddr() + " : " + req.getPathInfo(),
83 protected void doDelete(HttpServletRequest req, HttpServletResponse resp) {
85 common(req, resp, true);
86 } catch (IOException e) {
88 "SampleSubServlet: Failed to doDelete: "
96 * Process a PUT or DELETE request.
99 * <li>Verify that the request contains an Authorization header or else UNAUTHORIZED.
100 * <li>Verify that the Authorization header matches the configured Login and Password or else
102 * <li>If the request is PUT, store the message body as a file in the configured outputDirectory
103 * directory protecting against evil characters in the received FileID. The file is created
104 * initially with its name prefixed with a ".", and once it is complete, it is renamed to
105 * remove the leading "." character.
106 * <li>If the request is DELETE, instead delete the file (if it exists) from the configured
107 * outputDirectory directory.
108 * <li>Respond with NO_CONTENT.
111 private void common(HttpServletRequest req, HttpServletResponse resp, boolean isdelete)
113 String authHeader = req.getHeader("Authorization");
114 if (authHeader == null) {
116 "SampleSubServlet: Rejecting request with no Authorization header from "
117 + req.getRemoteAddr()
119 + req.getPathInfo());
120 resp.sendError(HttpServletResponse.SC_UNAUTHORIZED);
123 if (!basicAuth.equals(authHeader)) {
125 "SampleSubServlet: Rejecting request with incorrect Authorization header from "
126 + req.getRemoteAddr()
128 + req.getPathInfo());
129 resp.sendError(HttpServletResponse.SC_FORBIDDEN);
132 String fileid = req.getPathInfo();
133 fileid = fileid.substring(fileid.lastIndexOf('/') + 1);
134 String queryString = req.getQueryString();
135 if (queryString != null) {
136 fileid = fileid + "?" + queryString;
138 String publishid = req.getHeader("X-ATT-DR-PUBLISH-ID");
140 URLEncoder.encode(fileid, "UTF-8").replaceAll("^\\.", "%2E").replaceAll("\\*", "%2A");
141 String fullPath = outputDirectory + "/" + filename;
142 String tmpPath = outputDirectory + "/." + filename;
145 Files.deleteIfExists(Paths.get(fullPath));
147 "SampleSubServlet: Received delete for file id "
150 + req.getRemoteAddr()
156 new File(tmpPath).createNewFile();
157 try (InputStream is = req.getInputStream();
158 OutputStream os = new FileOutputStream(tmpPath)) {
159 byte[] buf = new byte[65536];
161 while ((i = is.read(buf)) > 0) {
165 Files.move(Paths.get(tmpPath), Paths.get(fullPath), StandardCopyOption.REPLACE_EXISTING);
167 "SampleSubServlet: Received file id "
170 + req.getRemoteAddr()
175 resp.setStatus(HttpServletResponse.SC_NO_CONTENT);
177 resp.setStatus(HttpServletResponse.SC_NO_CONTENT);
178 } catch (IOException ioe) {
179 Files.deleteIfExists(Paths.get(tmpPath));
181 "SampleSubServlet: Failed to process file "
184 + req.getRemoteAddr()
186 + req.getPathInfo());