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 ******************************************************************************/
24 package org.onap.dmaap.datarouter.subscriber;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
29 import java.io.FileOutputStream;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.io.OutputStream;
33 import java.io.PrintWriter;
34 import java.net.URLEncoder;
35 import java.nio.file.Files;
36 import java.nio.file.Paths;
37 import java.nio.file.StandardCopyOption;
38 import javax.servlet.http.HttpServlet;
39 import javax.servlet.http.HttpServletRequest;
40 import javax.servlet.http.HttpServletResponse;
41 import org.apache.commons.codec.binary.Base64;
44 public class SampleSubscriberServlet extends HttpServlet {
46 private static EELFLogger logger = EELFManager.getInstance().getLogger(SampleSubscriberServlet.class);
47 private static String outputDirectory;
48 private static String basicAuth;
51 * Configure the SampleSubscriberServlet.
54 * <li>Login - The login expected in the Authorization header (default "LOGIN").
55 * <li>Password - The password expected in the Authorization header (default "PASSWORD").
56 * <li>outputDirectory - The directory where files are placed (default
57 * "/opt/app/subscriber/delivery").
62 SubscriberProps props = SubscriberProps.getInstance();
63 String login = props.getValue("org.onap.dmaap.datarouter.subscriber.auth.user", "LOGIN");
64 String password = props.getValue("org.onap.dmaap.datarouter.subscriber.auth.password", "PASSWORD");
66 props.getValue("org.onap.dmaap.datarouter.subscriber.delivery.dir", "/opt/app/subscriber/delivery");
68 Files.createDirectory(Paths.get(outputDirectory));
69 } catch (IOException e) {
70 logger.info("SubServlet: Failed to create delivery dir: " + e.getMessage(), e);
72 basicAuth = "Basic " + Base64.encodeBase64String((login + ":" + password).getBytes());
76 protected void doPut(HttpServletRequest req, HttpServletResponse resp) {
78 common(req, resp, false);
79 } catch (IOException e) {
80 logger.info("SampleSubServlet: Failed to doPut: " + req.getRemoteAddr() + " : " + req.getPathInfo(), e);
85 protected void doDelete(HttpServletRequest req, HttpServletResponse resp) {
87 common(req, resp, true);
88 } catch (IOException e) {
89 logger.info("SampleSubServlet: Failed to doDelete: " + req.getRemoteAddr() + " : " + req.getPathInfo(), e);
94 * Process a PUT or DELETE request.
97 * <li>Verify that the request contains an Authorization header or else UNAUTHORIZED.
98 * <li>Verify that the Authorization header matches the configured Login and Password or else
100 * <li>If the request is PUT, store the message body as a file in the configured outputDirectory
101 * directory protecting against evil characters in the received FileID. The file is created
102 * initially with its name prefixed with a ".", and once it is complete, it is renamed to
103 * remove the leading "." character.
104 * <li>If the request is DELETE, instead delete the file (if it exists) from the configured
105 * outputDirectory directory.
106 * <li>Respond with NO_CONTENT.
109 private void common(HttpServletRequest req, HttpServletResponse resp, boolean isdelete) throws IOException {
110 String authHeader = req.getHeader("Authorization");
111 if (authHeader == null) {
112 logger.info("SampleSubServlet: Rejecting request with no Authorization header from " + req.getRemoteAddr()
113 + ": " + req.getPathInfo());
114 resp.sendError(HttpServletResponse.SC_UNAUTHORIZED);
117 if (!basicAuth.equals(authHeader)) {
118 logger.info("SampleSubServlet: Rejecting request with incorrect Authorization header from "
119 + req.getRemoteAddr() + ": " + req.getPathInfo());
120 resp.sendError(HttpServletResponse.SC_FORBIDDEN);
123 String fileid = req.getPathInfo();
124 fileid = fileid.substring(fileid.lastIndexOf('/') + 1);
125 String queryString = req.getQueryString();
126 if (queryString != null) {
127 fileid = fileid + "?" + queryString;
129 String publishid = req.getHeader("X-DMAAP-DR-PUBLISH-ID");
130 String filename = URLEncoder.encode(fileid, "UTF-8").replaceAll("^\\.", "%2E").replaceAll("\\*", "%2A");
131 String fullPath = outputDirectory + "/" + filename;
132 String tmpPath = outputDirectory + "/." + filename;
133 String fullMetaDataPath = outputDirectory + "/" + filename + ".M";
134 String tmpMetaDataPath = outputDirectory + "/." + filename + ".M";
137 Files.deleteIfExists(Paths.get(fullPath));
138 Files.deleteIfExists(Paths.get(fullMetaDataPath));
139 logger.info("SampleSubServlet: Received delete for file id " + fileid + " from " + req.getRemoteAddr()
140 + " publish id " + publishid + " as " + fullPath);
142 new File(tmpPath).createNewFile();
143 new File(tmpMetaDataPath).createNewFile();
144 try (InputStream is = req.getInputStream(); OutputStream os = new FileOutputStream(tmpPath)) {
145 byte[] buf = new byte[65536];
147 while ((bufferSize = is.read(buf)) > 0) {
148 os.write(buf, 0, bufferSize);
151 Files.move(Paths.get(tmpPath), Paths.get(fullPath), StandardCopyOption.REPLACE_EXISTING);
152 try (PrintWriter writer = new PrintWriter(new FileOutputStream(tmpMetaDataPath))) {
153 String metaData = req.getHeader("X-DMAAP-DR-META");
154 writer.print(metaData);
156 Files.move(Paths.get(tmpMetaDataPath), Paths.get(fullMetaDataPath),
157 StandardCopyOption.REPLACE_EXISTING);
159 "SampleSubServlet: Received file id " + fileid + " from " + req.getRemoteAddr() + " publish id "
160 + publishid + " as " + fullPath);
161 resp.setStatus(HttpServletResponse.SC_NO_CONTENT);
163 resp.setStatus(HttpServletResponse.SC_NO_CONTENT);
164 } catch (IOException ioe) {
165 Files.deleteIfExists(Paths.get(tmpPath));
166 Files.deleteIfExists(Paths.get(tmpMetaDataPath));
167 logger.info("SampleSubServlet: Failed to process file " + fullPath + " from " + req.getRemoteAddr() + ": "
168 + req.getPathInfo());