2  * ============LICENSE_START=======================================================
 
   3  * ONAP : ccsdk features
 
   4  * ================================================================================
 
   5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
 
   7  * ================================================================================
 
   8  * Licensed under the Apache License, Version 2.0 (the "License");
 
   9  * you may not use this file except in compliance with the License.
 
  10  * You may obtain a copy of the License at
 
  12  *     http://www.apache.org/licenses/LICENSE-2.0
 
  14  * Unless required by applicable law or agreed to in writing, software
 
  15  * distributed under the License is distributed on an "AS IS" BASIS,
 
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  17  * See the License for the specific language governing permissions and
 
  18  * limitations under the License.
 
  19  * ============LICENSE_END=========================================================
 
  22 package org.onap.ccsdk.features.sdnr.wt.dataprovider.http.yangschema;
 
  24 import java.io.IOException;
 
  25 import java.text.ParseException;
 
  26 import javax.servlet.Servlet;
 
  27 import javax.servlet.ServletException;
 
  28 import javax.servlet.http.HttpServlet;
 
  29 import javax.servlet.http.HttpServletRequest;
 
  30 import javax.servlet.http.HttpServletResponse;
 
  32 import org.osgi.service.component.annotations.Component;
 
  33 import org.osgi.service.http.whiteboard.propertytypes.HttpWhiteboardServletName;
 
  34 import org.osgi.service.http.whiteboard.propertytypes.HttpWhiteboardServletPattern;
 
  35 import org.slf4j.Logger;
 
  36 import org.slf4j.LoggerFactory;
 
  38 @HttpWhiteboardServletPattern("/yang-schema/*")
 
  39 @HttpWhiteboardServletName("YangSchemaHttpServlet")
 
  40 @Component(service = Servlet.class)
 
  41 public class YangSchemaHttpServlet extends HttpServlet {
 
  45     private static final long serialVersionUID = 1L;
 
  46     private static final Logger LOG = LoggerFactory.getLogger(YangSchemaHttpServlet.class);
 
  47     private static final String CACHEPATH = "cache/";
 
  48     private static final String SCHEMACACHEPATH_PRIMARY = CACHEPATH+"schema/";
 
  50     private final YangFileProvider fileProvider;
 
  52     public YangSchemaHttpServlet() {
 
  53         this.fileProvider = new YangFileProvider(CACHEPATH, SCHEMACACHEPATH_PRIMARY);
 
  57     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 
  59         GetYangSchemaRequest request = null;
 
  61             request = new GetYangSchemaRequest(req);
 
  62         } catch (Exception e) {
 
  63             LOG.warn("bad request for yang-schema: {}", e);
 
  65         if (request != null) {
 
  67             LOG.debug("request for yang-schema for module {} with version {}", request.getModule(),
 
  68                     request.getVersion());
 
  69             if (request.hasVersion()) {
 
  72                     has = this.fileProvider.hasFileOrNewerForModule(request.getModule(), request.getVersion());
 
  73                 } catch (ParseException e1) {
 
  74                     LOG.warn("unable to parse revision: {}", e1);
 
  79                         resp.setStatus(HttpServletResponse.SC_OK);
 
  80                         resp.setContentType("text/plain");
 
  81                         len = this.fileProvider.writeOutput(request.getModule(), request.getVersion(),
 
  82                                 resp.getOutputStream());
 
  83                         resp.setContentLength(len);
 
  85                     } catch (ParseException e) {
 
  86                         LOG.warn("unable to parse revision: {}", e);
 
  87                     } catch (IOException | IllegalStateException e) {
 
  88                         LOG.warn("unable to write out module {}@{}: {}", request.getModule(), request.getVersion(), e);
 
  92                         resp.sendError(HttpServletResponse.SC_NOT_FOUND);
 
  93                     } catch (IOException | IllegalStateException e) {
 
  94                         LOG.warn("unable to write out 404 res not found: {}", e);
 
  98             } else if (this.fileProvider.hasFileForModule(request.getModule())) {
 
 101                     resp.setStatus(HttpServletResponse.SC_OK);
 
 102                     resp.setContentType("text/plain");
 
 103                     len = this.fileProvider.writeOutput(request.getModule(), null, resp.getOutputStream());
 
 104                     resp.setContentLength(len);
 
 105                 } catch (ParseException e) {
 
 106                     LOG.warn(e.getMessage());
 
 107                 } catch (IOException | IllegalStateException e) {
 
 108                     LOG.warn("unable to write out module {}: {}", request.getModule(), e);
 
 113                     resp.sendError(HttpServletResponse.SC_NOT_FOUND);
 
 114                 } catch (IOException | IllegalStateException e) {
 
 115                     LOG.warn("unable to write out 404 res not found: {}", e);
 
 120                 resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
 
 121             } catch (IOException | IllegalStateException e) {
 
 122                 LOG.warn("unable to write out 400 bad request: {}", e);