1cb386505c6d551c8f7df251e200393b17c97fd4
[ccsdk/features.git] /
1 /*******************************************************************************
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt
4  * =================================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.dataprovider.http;
19
20 import java.io.IOException;
21 import java.text.ParseException;
22
23 import javax.servlet.ServletException;
24 import javax.servlet.http.HttpServlet;
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
27
28 import org.onap.ccsdk.features.sdnr.wt.dataprovider.data.YangFileProvider;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class YangSchemaHttpServlet extends HttpServlet {
33         /**
34          * 
35          */
36         private static final long serialVersionUID = 1L;
37         private static final Logger LOG = LoggerFactory.getLogger(YangSchemaHttpServlet.class);
38
39         private static final String schemaCachePath = "cache/schema/";
40
41         private final YangFileProvider fileProvider;
42
43         public YangSchemaHttpServlet() {
44                 this.fileProvider = new YangFileProvider(schemaCachePath);
45         }
46
47         @Override
48         protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
49
50                 GetYangSchemaRequest request = null;
51                 try {
52                         request = new GetYangSchemaRequest(req);
53                 } catch (Exception e) {
54                         LOG.warn("bad request for yang-schema: {}", e);
55                 }
56                 if (request != null) {
57                         int len;
58                         LOG.debug("request for yang-schema for module {} with version {}", request.getModule(),
59                                         request.getVersion());
60                         if (request.hasVersion()) {
61                                 boolean has=false;
62                                 try {
63                                         has = this.fileProvider.hasFileOrNewerForModule(request.getModule(), request.getVersion());
64                                 } catch (ParseException e1) {
65                                         LOG.warn("unable to parse revision: {}" ,e1);
66                                 }
67                                 if(has) {
68
69                                         try {
70                                                 resp.setStatus(HttpServletResponse.SC_OK);
71                                                 resp.setContentType("text/plain");
72                                                 len = this.fileProvider.writeOutput(request.getModule(), request.getVersion(),
73                                                                 resp.getOutputStream());
74                                                 resp.setContentLength(len);
75                                                 
76                                         } catch (ParseException e) {
77                                                 LOG.warn("unable to parse revision: {}", e);
78                                         }catch (IOException | IllegalStateException e) {
79                                                 LOG.warn("unable to write out module {}@{}: {}",request.getModule(), request.getVersion(), e);
80                                         }
81                                 } else {
82                                         try {
83                                                 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
84                                         }catch(IOException | IllegalStateException e) {
85                                                 LOG.warn("unable to write out 404 res not found: {}",e);
86                                         }
87                                 }
88
89                         } else if (this.fileProvider.hasFileForModule(request.getModule())) {
90
91                                 try {
92                                         resp.setStatus(HttpServletResponse.SC_OK);
93                                         resp.setContentType("text/plain");
94                                         len = this.fileProvider.writeOutput(request.getModule(), null, resp.getOutputStream());
95                                         resp.setContentLength(len);
96                                 } catch (ParseException e) {
97                                         LOG.warn(e.getMessage());
98                                 }catch(IOException | IllegalStateException e) {
99                                         LOG.warn("unable to write out module {}: {}",request.getModule(), e);
100                                 }
101
102                         } else {
103                                 try {
104                                         resp.sendError(HttpServletResponse.SC_NOT_FOUND);
105                                 }catch(IOException | IllegalStateException e) {
106                                         LOG.warn("unable to write out 404 res not found: {}",e);
107                                 }
108                         }
109                 } else {
110                         try {
111                                 resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
112                         }catch(IOException | IllegalStateException e) {
113                                 LOG.warn("unable to write out 400 bad request: {}",e);
114                         }
115                 }
116
117         }
118
119 }