2 * ============LICENSE_START===================================================
3 * Copyright (c) 2018 Amdocs
4 * ============================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 * ============LICENSE_END=====================================================
19 package org.onap.sdnc.apps.pomba.servicedecomposition.service.rs;
21 import static org.onap.sdnc.apps.pomba.servicedecomposition.exception.DiscoveryException.Error.*;
23 import java.util.UUID;
24 import javax.annotation.Resource;
25 import javax.servlet.http.HttpServletRequest;
26 import javax.ws.rs.core.HttpHeaders;
27 import javax.ws.rs.core.Response;
28 import javax.ws.rs.core.Response.ResponseBuilder;
29 import javax.ws.rs.core.Response.Status;
30 import org.onap.logging.ref.slf4j.ONAPLogAdapter;
31 import org.onap.logging.ref.slf4j.ONAPLogConstants;
32 import org.onap.logging.ref.slf4j.ONAPLogConstants.ResponseStatus;
33 import org.onap.sdnc.apps.pomba.servicedecomposition.exception.DiscoveryException;
34 import org.onap.sdnc.apps.pomba.servicedecomposition.service.SpringService;
35 import org.onap.sdnc.apps.pomba.servicedecomposition.util.RestUtil;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.stereotype.Component;
42 public class RestServiceImpl implements RestService {
43 private static Logger log = LoggerFactory.getLogger(RestServiceImpl.class);
44 private static final String EMPTY_JSON_OBJECT = "{}";
47 private SpringService service;
49 @Resource(name="basicAuthHeader")
50 private String basicAuthHeader;
53 public Response getContext(HttpServletRequest request,
57 String serviceInstanceId) {
59 ONAPLogAdapter adapter = new ONAPLogAdapter(log);
60 adapter.getServiceDescriptor().setServiceName(SERVICE_NAME);
61 adapter.entering(request);
63 if (authorization == null || !this.basicAuthHeader.equals(authorization)) {
64 throw new DiscoveryException(UNAUTHORIZED, Status.UNAUTHORIZED);
67 // Do some validation on Http headers and URL parameters
68 if ((fromAppId == null) || fromAppId.trim().isEmpty()) {
69 throw new DiscoveryException(MISSING_HEADER, Status.BAD_REQUEST, ONAPLogConstants.Headers.PARTNER_NAME);
71 if (transactionId == null || transactionId.isEmpty()) {
72 transactionId = UUID.randomUUID().toString();
73 log.debug("{} is missing; using newly generated value: {}", ONAPLogConstants.Headers.REQUEST_ID, transactionId);
75 RestUtil.validateURL(serviceInstanceId);
76 String context = service.decomposeService(fromAppId, transactionId, serviceInstanceId, adapter);
78 if (context == null) {
79 context = EMPTY_JSON_OBJECT;
81 adapter.getResponseDescriptor().setResponseStatus(ResponseStatus.COMPLETED);
82 return Response.ok().entity(context).build();
84 } catch (DiscoveryException x) {
85 log.error(x.getHttpStatus().getReasonPhrase(), x);
86 adapter.getResponseDescriptor()
87 .setResponseCode(x.getResponseCode())
88 .setResponseStatus(ResponseStatus.ERROR);
90 ResponseBuilder builder = Response.status(x.getHttpStatus()).entity(x.getMessage());
91 if (authorization == null) {
92 builder.header(HttpHeaders.WWW_AUTHENTICATE, "Basic realm=\""+ SERVICE_NAME + "\"");
94 return builder.build();
96 } catch (Exception e) {
97 log.error(Status.INTERNAL_SERVER_ERROR.getReasonPhrase(), e);
98 adapter.getResponseDescriptor()
99 .setResponseCode(GENERAL_FAILURE.getResponseCode())
100 .setResponseStatus(ResponseStatus.ERROR);
102 return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();