use of org.wso2.carbon.apimgt.impl.recommendationmgt.AccessTokenGenerator in project carbon-apimgt by wso2.
the class RecommenderDetailsExtractor method getRecommendations.
public String getRecommendations(String userName, String tenantDomain) {
String recommendationEndpointURL = recommendationEnvironment.getRecommendationServerURL() + APIConstants.RECOMMENDATIONS_GET_RESOURCE;
AccessTokenGenerator accessTokenGenerator = ServiceReferenceHolder.getInstance().getAccessTokenGenerator();
try {
String userID = apiMgtDAO.getUserID(userName);
URL serverURL = new URL(recommendationEndpointURL);
int serverPort = serverURL.getPort();
String serverProtocol = serverURL.getProtocol();
HttpGet method = new HttpGet(recommendationEndpointURL);
HttpClient httpClient = APIUtil.getHttpClient(serverPort, serverProtocol);
if (recommendationEnvironment.getOauthURL() != null) {
String accessToken = accessTokenGenerator.getAccessToken();
method.setHeader(APIConstants.AUTHORIZATION_HEADER_DEFAULT, APIConstants.AUTHORIZATION_BEARER + accessToken);
} else {
byte[] credentials = org.apache.commons.codec.binary.Base64.encodeBase64((recommendationEnvironment.getUserName() + ":" + recommendationEnvironment.getPassword()).getBytes(StandardCharsets.UTF_8));
method.setHeader(APIConstants.AUTHORIZATION_HEADER_DEFAULT, APIConstants.AUTHORIZATION_BASIC + new String(credentials, StandardCharsets.UTF_8));
}
method.setHeader(APIConstants.RECOMMENDATIONS_USER_HEADER, userID);
method.setHeader(APIConstants.RECOMMENDATIONS_ACCOUNT_HEADER, tenantDomain);
HttpResponse httpResponse = httpClient.execute(method);
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
log.info("Recommendations received for the user " + userName + " from recommendations server");
String contentString = EntityUtils.toString(httpResponse.getEntity());
if (log.isDebugEnabled()) {
log.debug("Recommendations received for user " + userName + " is " + contentString);
}
return contentString;
} else if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED && accessTokenGenerator != null) {
log.warn("Error getting recommendations from server. Invalid credentials used");
accessTokenGenerator.removeInvalidToken(new String[] { APIConstants.OAUTH2_DEFAULT_SCOPE });
} else {
log.warn("Error getting recommendations from server. Server responded with " + httpResponse.getStatusLine().getStatusCode());
}
} catch (IOException e) {
log.error("Connection failure for the recommendation engine", e);
} catch (APIManagementException e) {
log.error("Error while getting recommendations for user " + userName, e);
}
return null;
}
use of org.wso2.carbon.apimgt.impl.recommendationmgt.AccessTokenGenerator in project carbon-apimgt by wso2.
the class APIManagerComponent method setupAccessTokenGenerator.
private void setupAccessTokenGenerator() {
RecommendationEnvironment recommendationEnvironment = ServiceReferenceHolder.getInstance().getAPIManagerConfigurationService().getAPIManagerConfiguration().getApiRecommendationEnvironment();
if (recommendationEnvironment != null && recommendationEnvironment.getOauthURL() != null) {
AccessTokenGenerator accessTokenGenerator = new AccessTokenGenerator(recommendationEnvironment.getOauthURL(), recommendationEnvironment.getConsumerKey(), recommendationEnvironment.getConsumerSecret());
ServiceReferenceHolder.getInstance().setAccessTokenGenerator(accessTokenGenerator);
}
}
use of org.wso2.carbon.apimgt.impl.recommendationmgt.AccessTokenGenerator in project carbon-apimgt by wso2.
the class ExtendedHTTPEventAdapter method init.
@Override
public void init() throws OutputEventAdapterException {
tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
// ExecutorService will be assigned if it is null
if (executorService == null) {
int minThread;
int maxThread;
long defaultKeepAliveTime;
int jobQueSize;
// If global properties are available those will be assigned else constant values will be assigned
if (globalProperties.get(ExtendedHTTPEventAdapterConstants.ADAPTER_MIN_THREAD_POOL_SIZE_NAME) != null) {
minThread = Integer.parseInt(globalProperties.get(ExtendedHTTPEventAdapterConstants.ADAPTER_MIN_THREAD_POOL_SIZE_NAME));
} else {
minThread = ExtendedHTTPEventAdapterConstants.ADAPTER_MIN_THREAD_POOL_SIZE;
}
if (globalProperties.get(ExtendedHTTPEventAdapterConstants.ADAPTER_MAX_THREAD_POOL_SIZE_NAME) != null) {
maxThread = Integer.parseInt(globalProperties.get(ExtendedHTTPEventAdapterConstants.ADAPTER_MAX_THREAD_POOL_SIZE_NAME));
} else {
maxThread = ExtendedHTTPEventAdapterConstants.ADAPTER_MAX_THREAD_POOL_SIZE;
}
if (globalProperties.get(ExtendedHTTPEventAdapterConstants.ADAPTER_KEEP_ALIVE_TIME_NAME) != null) {
defaultKeepAliveTime = Integer.parseInt(globalProperties.get(ExtendedHTTPEventAdapterConstants.ADAPTER_KEEP_ALIVE_TIME_NAME));
} else {
defaultKeepAliveTime = ExtendedHTTPEventAdapterConstants.DEFAULT_KEEP_ALIVE_TIME_IN_MILLIS;
}
if (globalProperties.get(ExtendedHTTPEventAdapterConstants.ADAPTER_EXECUTOR_JOB_QUEUE_SIZE_NAME) != null) {
jobQueSize = Integer.parseInt(globalProperties.get(ExtendedHTTPEventAdapterConstants.ADAPTER_EXECUTOR_JOB_QUEUE_SIZE_NAME));
} else {
jobQueSize = ExtendedHTTPEventAdapterConstants.ADAPTER_EXECUTOR_JOB_QUEUE_SIZE;
}
executorService = new ThreadPoolExecutor(minThread, maxThread, defaultKeepAliveTime, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(jobQueSize));
// configurations for the httpConnectionManager which will be shared by every http adapter
int defaultMaxConnectionsPerHost;
int maxTotalConnections;
if (globalProperties.get(ExtendedHTTPEventAdapterConstants.DEFAULT_MAX_CONNECTIONS_PER_HOST) != null) {
defaultMaxConnectionsPerHost = Integer.parseInt(globalProperties.get(ExtendedHTTPEventAdapterConstants.DEFAULT_MAX_CONNECTIONS_PER_HOST));
} else {
defaultMaxConnectionsPerHost = ExtendedHTTPEventAdapterConstants.DEFAULT_DEFAULT_MAX_CONNECTIONS_PER_HOST;
}
if (globalProperties.get(ExtendedHTTPEventAdapterConstants.MAX_TOTAL_CONNECTIONS) != null) {
maxTotalConnections = Integer.parseInt(globalProperties.get(ExtendedHTTPEventAdapterConstants.MAX_TOTAL_CONNECTIONS));
} else {
maxTotalConnections = ExtendedHTTPEventAdapterConstants.DEFAULT_MAX_TOTAL_CONNECTIONS;
}
connectionManager = new MultiThreadedHttpConnectionManager();
connectionManager.getParams().setDefaultMaxConnectionsPerHost(defaultMaxConnectionsPerHost);
connectionManager.getParams().setMaxTotalConnections(maxTotalConnections);
Map<String, String> staticProperties = eventAdapterConfiguration.getStaticProperties();
if (staticProperties.get(ExtendedHTTPEventAdapterConstants.ADAPTER_OAUTH_CONSUMER_KEY) != null) {
accessTokenGenerator = new AccessTokenGenerator(staticProperties.get(ExtendedHTTPEventAdapterConstants.ADAPTER_OAUTH_URL), staticProperties.get(ExtendedHTTPEventAdapterConstants.ADAPTER_OAUTH_CONSUMER_KEY), staticProperties.get(ExtendedHTTPEventAdapterConstants.ADAPTER_OAUTH_CONSUMER_SECRET));
this.oauthURL = staticProperties.get(ExtendedHTTPEventAdapterConstants.ADAPTER_OAUTH_URL);
}
}
}
Aggregations