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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
16 * ============LICENSE_END==========================================================================
17 ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.dataprovider.http;
20 import java.io.IOException;
21 import java.text.ParseException;
23 import javax.servlet.ServletException;
24 import javax.servlet.http.HttpServlet;
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
28 import org.onap.ccsdk.features.sdnr.wt.dataprovider.data.YangFileProvider;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 public class YangSchemaHttpServlet extends HttpServlet {
36 private static final long serialVersionUID = 1L;
37 private static final Logger LOG = LoggerFactory.getLogger(YangSchemaHttpServlet.class);
39 private static final String schemaCachePath = "cache/schema/";
41 private final YangFileProvider fileProvider;
43 public YangSchemaHttpServlet() {
44 this.fileProvider = new YangFileProvider(schemaCachePath);
48 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
50 GetYangSchemaRequest request = null;
52 request = new GetYangSchemaRequest(req);
53 } catch (Exception e) {
54 LOG.warn("bad request for yang-schema: {}", e);
56 if (request != null) {
58 LOG.debug("request for yang-schema for module {} with version {}", request.getModule(),
59 request.getVersion());
60 if (request.hasVersion()) {
63 has = this.fileProvider.hasFileOrNewerForModule(request.getModule(), request.getVersion());
64 } catch (ParseException e1) {
65 LOG.warn("unable to parse revision: {}" ,e1);
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);
76 } catch (ParseException e) {
77 LOG.warn("unable to parse revision: {}", e);
80 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
83 } else if (this.fileProvider.hasFileForModule(request.getModule())) {
86 resp.setStatus(HttpServletResponse.SC_OK);
87 resp.setContentType("text/plain");
88 len = this.fileProvider.writeOutput(request.getModule(), null, resp.getOutputStream());
89 resp.setContentLength(len);
90 } catch (ParseException e) {
91 LOG.warn(e.getMessage());
95 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
98 resp.sendError(HttpServletResponse.SC_BAD_REQUEST);