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