summaryrefslogtreecommitdiffstats
path: root/ecomp-sdk/epsdk-core/src/main/java/org/openecomp/portalsdk/core/service/RestApiRequestBuilder.java
blob: 00673682d964d75759aa6fdec459656ab56a80ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package org.openecomp.portalsdk.core.service;

import static com.att.eelf.configuration.Configuration.MDC_KEY_REQUEST_ID;

import org.openecomp.portalsdk.core.domain.App;
import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
import org.openecomp.portalsdk.core.onboarding.rest.RestWebServiceClient;
import org.openecomp.portalsdk.core.onboarding.util.CipherUtil;
import org.openecomp.portalsdk.core.util.SystemProperties;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;

public class RestApiRequestBuilder {
	
	private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RestApiRequestBuilder.class);
	
	@Autowired
	AppService appService;
	
	public static String content_type = "application/json";
	
	public String getViaREST(String restEndPoint, boolean isBasicAuth,String userId) throws Exception {
		String appName = "";
		String requestId = "";
		String appUserName = "";
		String decryptedPwd = "";

		logger.info(EELFLoggerDelegate.debugLogger, "Making use of REST API communication for GET" + restEndPoint);

		App app = appService.getDefaultApp();

		if (app != null) {
			appName = app.getName();
			appUserName = app.getUsername();
			try {
				decryptedPwd = CipherUtil.decrypt(app.getAppPassword(),
						SystemProperties.getProperty(SystemProperties.Decryption_Key));
			} catch (Exception e) {
				logger.error(EELFLoggerDelegate.errorLogger,
						"Exception occurred in WebServiceCallServiceImpl.get while decrypting the password. Details: "
								+ e.toString());
			}
		} else {
			logger.warn(EELFLoggerDelegate.errorLogger, "Unable to locate the app information from the database.");
			appName = SystemProperties.SDK_NAME;
		}
		requestId = MDC.get(MDC_KEY_REQUEST_ID);

		String response = null;
		try {
			response =	RestWebServiceClient.getInstance().getPortalContent(restEndPoint, userId,appName, requestId, appUserName,
					decryptedPwd, isBasicAuth);
		} catch (Exception ex) {
			response = "Failed to perform GET " + ex.toString();
			throw new Exception("get Failed"+ ex);
		}
		logger.debug(EELFLoggerDelegate.errorLogger, "getRoles response: {}", response);
		return response;
	}
	
	public void postViaREST(String restEndPoint, boolean isBasicAuth, String content,String userId) throws Exception {
		String appName = "";
		String requestId = "";
		String appUserName = "";
		String decryptedPwd = "";

		logger.info(EELFLoggerDelegate.debugLogger, "Making use of REST API communication for POST" + restEndPoint);

		App app = appService.getDefaultApp();

		if (app != null) {
			appName = app.getName();
			appUserName = app.getUsername();
			try {
				decryptedPwd = CipherUtil.decrypt(app.getAppPassword(),
						SystemProperties.getProperty(SystemProperties.Decryption_Key));
			} catch (Exception e) {
				logger.error(EELFLoggerDelegate.errorLogger,
						"Exception occurred in WebServiceCallServiceImpl.get while decrypting the password. Details: "
								+ e.toString());
			}
		} else {
			logger.warn(EELFLoggerDelegate.errorLogger, "Unable to locate the app information from the database.");
			appName = SystemProperties.SDK_NAME;
		}
		requestId = MDC.get(MDC_KEY_REQUEST_ID);

		
		try {
			RestWebServiceClient.getInstance().postPortalContent(restEndPoint, userId, appName, requestId, appUserName,
					decryptedPwd, content_type, content, isBasicAuth);
		} catch (Exception ex) {
			logger.error(EELFLoggerDelegate.errorLogger, "POST response: {}", ex);

			throw new Exception("Save Failed");
		}
		logger.debug(EELFLoggerDelegate.debugLogger, "POST response: {}");
		 
	}
	
	public void deleteViaRest(String restEndPoint, boolean isBasicAuth, String content, String filter , String userId) throws Exception {
		String appName = "";
		String requestId = "";
		String appUserName = "";
		String decryptedPwd = "";

		logger.info(EELFLoggerDelegate.debugLogger, "Making use of REST API communication for DELETE" + restEndPoint);

		App app = appService.getDefaultApp();

		if (app != null) {
			appName = app.getName();
			appUserName = app.getUsername();
			try {
				decryptedPwd = CipherUtil.decrypt(app.getAppPassword(),
						SystemProperties.getProperty(SystemProperties.Decryption_Key));
			} catch (Exception e) {
				logger.error(EELFLoggerDelegate.errorLogger,
						"Exception occurred in WebServiceCallServiceImpl.get while decrypting the password. Details: "
								+ e.toString());
			}
		} else {
			logger.warn(EELFLoggerDelegate.errorLogger, "Unable to locate the app information from the database.");
			appName = SystemProperties.SDK_NAME;
		}
		requestId = MDC.get(MDC_KEY_REQUEST_ID);

		
		try {
			RestWebServiceClient.getInstance().deletePortalContent(restEndPoint, userId, appName, requestId, appUserName,
					decryptedPwd, content_type, content, isBasicAuth, filter);
		} catch (Exception ex) {
			logger.error(EELFLoggerDelegate.errorLogger, "DELETE response: {}", ex);
			throw new Exception("Delete Failed");
		}
		logger.debug(EELFLoggerDelegate.debugLogger, "DELETE response: {}");
		 
	}

}