update sparky with configurable features
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / personalization / PersonalizationServiceProvider.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
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 package org.onap.aai.sparky.personalization;
22
23 import javax.servlet.http.HttpServletRequest;
24
25 import org.apache.camel.Exchange;
26 import org.json.JSONObject;
27 import org.onap.aai.cl.api.Logger;
28 import org.onap.aai.cl.eelf.LoggerFactory;
29 import org.onap.aai.restclient.client.OperationResult;
30 import org.onap.aai.sparky.logging.AaiUiMsgs;
31 import org.onap.aai.sparky.logging.util.ServletUtils;
32 import org.onap.aai.sparky.personalization.config.PersonalizationConfig;
33
34 public class PersonalizationServiceProvider {
35
36   private static final String EMPTY_RESPONSE = "{}";
37
38   private static final Logger LOG =
39       LoggerFactory.getInstance().getLogger(PersonalizationServiceProvider.class);
40
41   PersonalizationConfig personalizationConfig;
42
43
44   public PersonalizationServiceProvider(PersonalizationConfig personalizationConfig) {
45     this.personalizationConfig = personalizationConfig;
46   }
47
48
49   public void getPersonalizedValues(Exchange exchange) {
50
51     HttpServletRequest request = exchange.getIn().getBody(HttpServletRequest.class);
52     ServletUtils.setUpMdcContext(exchange, request);
53
54     OperationResult personalizedValuesResult = new OperationResult();
55
56     try {
57       personalizedValuesResult.setResultCode(200);
58       JSONObject personalizedValuesResponse = new JSONObject();
59       personalizedValuesResponse.put("topLeftHeader", personalizationConfig.getTopLeftHeader());
60       personalizedValuesResponse.put("htmlDocumentTitle",
61           personalizationConfig.getHtmlDocumentTitle());
62       personalizedValuesResult.setResult(personalizedValuesResponse.toString());
63
64     } catch (Exception exc) {
65       personalizedValuesResult.setResultCode(500);
66       personalizedValuesResult.setResult(EMPTY_RESPONSE);
67       LOG.error(AaiUiMsgs.FAILURE_TO_PROCESS_REQUEST,
68           "Exception thrown during personalization processing: " + exc.getLocalizedMessage());
69     }
70
71     exchange.getOut().setBody(personalizedValuesResult.getResult());
72
73
74   }
75
76 }