075f4f766464929df12bd9e60dc3564e5ce0c46c
[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;
23
24 import java.io.IOException;
25 import java.text.ParseException;
26
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.onap.ccsdk.features.sdnr.wt.dataprovider.data.YangFileProvider;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public class YangSchemaHttpServlet extends HttpServlet {
37     /**
38      * 
39      */
40     private static final long serialVersionUID = 1L;
41     private static final Logger LOG = LoggerFactory.getLogger(YangSchemaHttpServlet.class);
42
43     private static final String schemaCachePath = "cache/schema/";
44
45     private final YangFileProvider fileProvider;
46
47     public YangSchemaHttpServlet() {
48         this.fileProvider = new YangFileProvider(schemaCachePath);
49     }
50
51     @Override
52     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
53
54         GetYangSchemaRequest request = null;
55         try {
56             request = new GetYangSchemaRequest(req);
57         } catch (Exception e) {
58             LOG.warn("bad request for yang-schema: {}", e);
59         }
60         if (request != null) {
61             int len;
62             LOG.debug("request for yang-schema for module {} with version {}", request.getModule(),
63                     request.getVersion());
64             if (request.hasVersion()) {
65                 boolean has = false;
66                 try {
67                     has = this.fileProvider.hasFileOrNewerForModule(request.getModule(), request.getVersion());
68                 } catch (ParseException e1) {
69                     LOG.warn("unable to parse revision: {}", e1);
70                 }
71                 if (has) {
72
73                     try {
74                         resp.setStatus(HttpServletResponse.SC_OK);
75                         resp.setContentType("text/plain");
76                         len = this.fileProvider.writeOutput(request.getModule(), request.getVersion(),
77                                 resp.getOutputStream());
78                         resp.setContentLength(len);
79
80                     } catch (ParseException e) {
81                         LOG.warn("unable to parse revision: {}", e);
82                     } catch (IOException | IllegalStateException e) {
83                         LOG.warn("unable to write out module {}@{}: {}", request.getModule(), request.getVersion(), e);
84                     }
85                 } else {
86                     try {
87                         resp.sendError(HttpServletResponse.SC_NOT_FOUND);
88                     } catch (IOException | IllegalStateException e) {
89                         LOG.warn("unable to write out 404 res not found: {}", e);
90                     }
91                 }
92
93             } else if (this.fileProvider.hasFileForModule(request.getModule())) {
94
95                 try {
96                     resp.setStatus(HttpServletResponse.SC_OK);
97                     resp.setContentType("text/plain");
98                     len = this.fileProvider.writeOutput(request.getModule(), null, resp.getOutputStream());
99                     resp.setContentLength(len);
100                 } catch (ParseException e) {
101                     LOG.warn(e.getMessage());
102                 } catch (IOException | IllegalStateException e) {
103                     LOG.warn("unable to write out module {}: {}", request.getModule(), e);
104                 }
105
106             } else {
107                 try {
108                     resp.sendError(HttpServletResponse.SC_NOT_FOUND);
109                 } catch (IOException | IllegalStateException e) {
110                     LOG.warn("unable to write out 404 res not found: {}", e);
111                 }
112             }
113         } else {
114             try {
115                 resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
116             } catch (IOException | IllegalStateException e) {
117                 LOG.warn("unable to write out 400 bad request: {}", e);
118             }
119         }
120
121     }
122
123 }