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