f9fa2e931103cf91d0358afb3626f6f22e458802
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
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
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  *
21  */
22 package org.onap.ccsdk.features.sdnr.wt.dataprovider.http.yangschema;
23
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;
31
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;
37
38 @HttpWhiteboardServletPattern("/yang-schema/*")
39 @HttpWhiteboardServletName("YangSchemaHttpServlet")
40 @Component(service = Servlet.class)
41 public class YangSchemaHttpServlet extends HttpServlet {
42     /**
43      *
44      */
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/";
49
50     private final YangFileProvider fileProvider;
51
52     public YangSchemaHttpServlet() {
53         this.fileProvider = new YangFileProvider(CACHEPATH, SCHEMACACHEPATH_PRIMARY);
54     }
55
56     @Override
57     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
58
59         GetYangSchemaRequest request = null;
60         try {
61             request = new GetYangSchemaRequest(req);
62         } catch (Exception e) {
63             LOG.warn("bad request for yang-schema: {}", e);
64         }
65         if (request != null) {
66             int len;
67             LOG.debug("request for yang-schema for module {} with version {}", request.getModule(),
68                     request.getVersion());
69             if (request.hasVersion()) {
70                 boolean has = false;
71                 try {
72                     has = this.fileProvider.hasFileOrNewerForModule(request.getModule(), request.getVersion());
73                 } catch (ParseException e1) {
74                     LOG.warn("unable to parse revision: {}", e1);
75                 }
76                 if (has) {
77
78                     try {
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);
84
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);
89                     }
90                 } else {
91                     try {
92                         resp.sendError(HttpServletResponse.SC_NOT_FOUND);
93                     } catch (IOException | IllegalStateException e) {
94                         LOG.warn("unable to write out 404 res not found: {}", e);
95                     }
96                 }
97
98             } else if (this.fileProvider.hasFileForModule(request.getModule())) {
99
100                 try {
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);
109                 }
110
111             } else {
112                 try {
113                     resp.sendError(HttpServletResponse.SC_NOT_FOUND);
114                 } catch (IOException | IllegalStateException e) {
115                     LOG.warn("unable to write out 404 res not found: {}", e);
116                 }
117             }
118         } else {
119             try {
120                 resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
121             } catch (IOException | IllegalStateException e) {
122                 LOG.warn("unable to write out 400 bad request: {}", e);
123             }
124         }
125
126     }
127
128 }