X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=datarouter-subscriber%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fdmaap%2Fdatarouter%2Fsubscriber%2FSampleSubscriberServlet.java;h=993d32532ba0b12f6a28fd5b64f52fa92e05ef0f;hb=1c11dffada48c31491f2583f0d28ccecb4b4891b;hp=e9a0bf9bb93bb726cda525bd11c6dd9cc2d7dcdc;hpb=f94a3f35ccf9f95455b2840e4465a71b0bb5c71d;p=dmaap%2Fdatarouter.git diff --git a/datarouter-subscriber/src/main/java/org/onap/dmaap/datarouter/subscriber/SampleSubscriberServlet.java b/datarouter-subscriber/src/main/java/org/onap/dmaap/datarouter/subscriber/SampleSubscriberServlet.java index e9a0bf9b..993d3253 100644 --- a/datarouter-subscriber/src/main/java/org/onap/dmaap/datarouter/subscriber/SampleSubscriberServlet.java +++ b/datarouter-subscriber/src/main/java/org/onap/dmaap/datarouter/subscriber/SampleSubscriberServlet.java @@ -20,181 +20,152 @@ * * ECOMP is a trademark and service mark of AT&T Intellectual Property. * * ******************************************************************************/ -package org.onap.dmaap.datarouter.subscriber; -import org.apache.commons.codec.binary.Base64; -import org.apache.log4j.Logger; +package org.onap.dmaap.datarouter.subscriber; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.*; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.PrintWriter; import java.net.URLEncoder; import java.nio.file.Files; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import org.apache.commons.codec.binary.Base64; +import org.apache.log4j.Logger; + public class SampleSubscriberServlet extends HttpServlet { - private static Logger logger = - Logger.getLogger("org.onap.dmaap.datarouter.subscriber.SampleSubscriberServlet"); - private static String outputDirectory; - private static String basicAuth; + private static Logger logger = Logger.getLogger("org.onap.dmaap.datarouter.subscriber.SampleSubscriberServlet"); + private static String outputDirectory; + private static String basicAuth; - /** - * Configure the SampleSubscriberServlet. - * - * - */ - @Override - public void init() { - SubscriberProps props = SubscriberProps.getInstance(); - String login = props.getValue("org.onap.dmaap.datarouter.subscriber.auth.user", "LOGIN"); - String password = - props.getValue("org.onap.dmaap.datarouter.subscriber.auth.password", "PASSWORD"); - outputDirectory = - props.getValue( - "org.onap.dmaap.datarouter.subscriber.delivery.dir", "/opt/app/subscriber/delivery"); - try { - Files.createDirectory(Paths.get(outputDirectory)); - } catch (IOException e) { - logger.info("SubServlet: Failed to create delivery dir: " + e.getMessage(), e); + /** + * Configure the SampleSubscriberServlet. + * + * + */ + @Override + public void init() { + SubscriberProps props = SubscriberProps.getInstance(); + String login = props.getValue("org.onap.dmaap.datarouter.subscriber.auth.user", "LOGIN"); + String password = props.getValue("org.onap.dmaap.datarouter.subscriber.auth.password", "PASSWORD"); + outputDirectory = + props.getValue("org.onap.dmaap.datarouter.subscriber.delivery.dir", "/opt/app/subscriber/delivery"); + try { + Files.createDirectory(Paths.get(outputDirectory)); + } catch (IOException e) { + logger.info("SubServlet: Failed to create delivery dir: " + e.getMessage(), e); + } + basicAuth = "Basic " + Base64.encodeBase64String((login + ":" + password).getBytes()); } - basicAuth = "Basic " + Base64.encodeBase64String((login + ":" + password).getBytes()); - } - @Override - protected void doPut(HttpServletRequest req, HttpServletResponse resp) { - try { - common(req, resp, false); - } catch (IOException e) { - logger.info( - "SampleSubServlet: Failed to doPut: " + req.getRemoteAddr() + " : " + req.getPathInfo(), - e); + @Override + protected void doPut(HttpServletRequest req, HttpServletResponse resp) { + try { + common(req, resp, false); + } catch (IOException e) { + logger.info("SampleSubServlet: Failed to doPut: " + req.getRemoteAddr() + " : " + req.getPathInfo(), e); + } } - } - @Override - protected void doDelete(HttpServletRequest req, HttpServletResponse resp) { - try { - common(req, resp, true); - } catch (IOException e) { - logger.info( - "SampleSubServlet: Failed to doDelete: " - + req.getRemoteAddr() - + " : " - + req.getPathInfo(), - e); - } - } - /** - * Process a PUT or DELETE request. - * - *
    - *
  1. Verify that the request contains an Authorization header or else UNAUTHORIZED. - *
  2. Verify that the Authorization header matches the configured Login and Password or else - * FORBIDDEN. - *
  3. If the request is PUT, store the message body as a file in the configured outputDirectory - * directory protecting against evil characters in the received FileID. The file is created - * initially with its name prefixed with a ".", and once it is complete, it is renamed to - * remove the leading "." character. - *
  4. If the request is DELETE, instead delete the file (if it exists) from the configured - * outputDirectory directory. - *
  5. Respond with NO_CONTENT. - *
- */ - private void common(HttpServletRequest req, HttpServletResponse resp, boolean isdelete) - throws IOException { - String authHeader = req.getHeader("Authorization"); - if (authHeader == null) { - logger.info( - "SampleSubServlet: Rejecting request with no Authorization header from " - + req.getRemoteAddr() - + ": " - + req.getPathInfo()); - resp.sendError(HttpServletResponse.SC_UNAUTHORIZED); - return; - } - if (!basicAuth.equals(authHeader)) { - logger.info( - "SampleSubServlet: Rejecting request with incorrect Authorization header from " - + req.getRemoteAddr() - + ": " - + req.getPathInfo()); - resp.sendError(HttpServletResponse.SC_FORBIDDEN); - return; - } - String fileid = req.getPathInfo(); - fileid = fileid.substring(fileid.lastIndexOf('/') + 1); - String queryString = req.getQueryString(); - if (queryString != null) { - fileid = fileid + "?" + queryString; + @Override + protected void doDelete(HttpServletRequest req, HttpServletResponse resp) { + try { + common(req, resp, true); + } catch (IOException e) { + logger.info("SampleSubServlet: Failed to doDelete: " + req.getRemoteAddr() + " : " + req.getPathInfo(), e); + } } - String publishid = req.getHeader("X-DMAAP-DR-PUBLISH-ID"); - String filename = - URLEncoder.encode(fileid, "UTF-8").replaceAll("^\\.", "%2E").replaceAll("\\*", "%2A"); - String fullPath = outputDirectory + "/" + filename; - String tmpPath = outputDirectory + "/." + filename; - String fullMetaDataPath = outputDirectory + "/" + filename + ".M"; - String tmpMetaDataPath = outputDirectory + "/." + filename + ".M"; - try { - if (isdelete) { - Files.deleteIfExists(Paths.get(fullPath)); - Files.deleteIfExists(Paths.get(fullMetaDataPath)); - logger.info( - "SampleSubServlet: Received delete for file id " - + fileid - + " from " - + req.getRemoteAddr() - + " publish id " - + publishid - + " as " - + fullPath); - } else { - new File(tmpPath).createNewFile(); - new File(tmpMetaDataPath).createNewFile(); - try (InputStream is = req.getInputStream(); - OutputStream os = new FileOutputStream(tmpPath)) { - byte[] buf = new byte[65536]; - int i; - while ((i = is.read(buf)) > 0) { - os.write(buf, 0, i); - } + + /** + * Process a PUT or DELETE request. + * + *
    + *
  1. Verify that the request contains an Authorization header or else UNAUTHORIZED. + *
  2. Verify that the Authorization header matches the configured Login and Password or else + * FORBIDDEN. + *
  3. If the request is PUT, store the message body as a file in the configured outputDirectory + * directory protecting against evil characters in the received FileID. The file is created + * initially with its name prefixed with a ".", and once it is complete, it is renamed to + * remove the leading "." character. + *
  4. If the request is DELETE, instead delete the file (if it exists) from the configured + * outputDirectory directory. + *
  5. Respond with NO_CONTENT. + *
+ */ + private void common(HttpServletRequest req, HttpServletResponse resp, boolean isdelete) throws IOException { + String authHeader = req.getHeader("Authorization"); + if (authHeader == null) { + logger.info("SampleSubServlet: Rejecting request with no Authorization header from " + req.getRemoteAddr() + + ": " + req.getPathInfo()); + resp.sendError(HttpServletResponse.SC_UNAUTHORIZED); + return; + } + if (!basicAuth.equals(authHeader)) { + logger.info("SampleSubServlet: Rejecting request with incorrect Authorization header from " + + req.getRemoteAddr() + ": " + req.getPathInfo()); + resp.sendError(HttpServletResponse.SC_FORBIDDEN); + return; + } + String fileid = req.getPathInfo(); + fileid = fileid.substring(fileid.lastIndexOf('/') + 1); + String queryString = req.getQueryString(); + if (queryString != null) { + fileid = fileid + "?" + queryString; } - Files.move(Paths.get(tmpPath), Paths.get(fullPath), StandardCopyOption.REPLACE_EXISTING); - try (PrintWriter writer = new PrintWriter(new FileOutputStream(tmpMetaDataPath))) { - String metaData = req.getHeader("X-DMAAP-DR-META"); - writer.print(metaData); + String publishid = req.getHeader("X-DMAAP-DR-PUBLISH-ID"); + String filename = URLEncoder.encode(fileid, "UTF-8").replaceAll("^\\.", "%2E").replaceAll("\\*", "%2A"); + String fullPath = outputDirectory + "/" + filename; + String tmpPath = outputDirectory + "/." + filename; + String fullMetaDataPath = outputDirectory + "/" + filename + ".M"; + String tmpMetaDataPath = outputDirectory + "/." + filename + ".M"; + try { + if (isdelete) { + Files.deleteIfExists(Paths.get(fullPath)); + Files.deleteIfExists(Paths.get(fullMetaDataPath)); + logger.info("SampleSubServlet: Received delete for file id " + fileid + " from " + req.getRemoteAddr() + + " publish id " + publishid + " as " + fullPath); + } else { + new File(tmpPath).createNewFile(); + new File(tmpMetaDataPath).createNewFile(); + try (InputStream is = req.getInputStream(); OutputStream os = new FileOutputStream(tmpPath)) { + byte[] buf = new byte[65536]; + int bufferSize; + while ((bufferSize = is.read(buf)) > 0) { + os.write(buf, 0, bufferSize); + } + } + Files.move(Paths.get(tmpPath), Paths.get(fullPath), StandardCopyOption.REPLACE_EXISTING); + try (PrintWriter writer = new PrintWriter(new FileOutputStream(tmpMetaDataPath))) { + String metaData = req.getHeader("X-DMAAP-DR-META"); + writer.print(metaData); + } + Files.move(Paths.get(tmpMetaDataPath), Paths.get(fullMetaDataPath), + StandardCopyOption.REPLACE_EXISTING); + logger.info( + "SampleSubServlet: Received file id " + fileid + " from " + req.getRemoteAddr() + " publish id " + + publishid + " as " + fullPath); + resp.setStatus(HttpServletResponse.SC_NO_CONTENT); + } + resp.setStatus(HttpServletResponse.SC_NO_CONTENT); + } catch (IOException ioe) { + Files.deleteIfExists(Paths.get(tmpPath)); + Files.deleteIfExists(Paths.get(tmpMetaDataPath)); + logger.info("SampleSubServlet: Failed to process file " + fullPath + " from " + req.getRemoteAddr() + ": " + + req.getPathInfo()); + throw ioe; } - Files.move(Paths.get(tmpMetaDataPath), Paths.get(fullMetaDataPath), StandardCopyOption.REPLACE_EXISTING); - logger.info( - "SampleSubServlet: Received file id " - + fileid - + " from " - + req.getRemoteAddr() - + " publish id " - + publishid - + " as " - + fullPath); - resp.setStatus(HttpServletResponse.SC_NO_CONTENT); - } - resp.setStatus(HttpServletResponse.SC_NO_CONTENT); - } catch (IOException ioe) { - Files.deleteIfExists(Paths.get(tmpPath)); - Files.deleteIfExists(Paths.get(tmpMetaDataPath)); - logger.info( - "SampleSubServlet: Failed to process file " - + fullPath - + " from " - + req.getRemoteAddr() - + ": " - + req.getPathInfo()); - throw ioe; } - } }