Created
April 25, 2018 06:57
-
-
Save sebge2emasphere/b8efa9f26fd2c0a57dba29c5fd20c31d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.emasphere.integrationgateway.proxy.operation.connector; | |
import com.emasphere.directory.client.EmaAuthenticationToken; | |
import com.emasphere.integrationgateway.model.operation.connector.ConnectorRequestContext; | |
import com.emasphere.integrationgateway.operation.model.connector.flow.FlowDefinitionReference; | |
import com.emasphere.integrationgateway.operation.model.connector.flow.FlowDefinitionValidationResult; | |
import com.emasphere.integrationgateway.operation.model.connector.validation.ValidationResult; | |
import com.emasphere.integrationgateway.proxy.support.RemotingException; | |
import org.springframework.core.ParameterizedTypeReference; | |
import org.springframework.http.HttpEntity; | |
import org.springframework.http.HttpHeaders; | |
import org.springframework.http.HttpMethod; | |
import org.springframework.http.HttpStatus; | |
import org.springframework.http.ResponseEntity; | |
import org.springframework.security.core.Authentication; | |
import org.springframework.security.core.context.SecurityContextHolder; | |
import org.springframework.stereotype.Service; | |
import org.springframework.web.client.RestTemplate; | |
import java.util.List; | |
import java.util.Map; | |
import static java.util.Arrays.*; | |
import static org.springframework.http.HttpHeaders.COOKIE; | |
/** | |
* Implementation of the {@link ConnectorServiceProxy connector service proxy}. | |
* | |
* @author Sebastien Gerard | |
*/ | |
@Service | |
public class ConnectorServiceProxyImpl implements ConnectorServiceProxy { | |
/** | |
* The HTTP header containing the access key to use. | |
*/ | |
public static final String ACCESS_KEY_HEADER = "ACCESS_KEY"; | |
/** | |
* The HTTP header containing the user name. | |
*/ | |
public static final String USER_NAME_HEADER = "USER_NAME"; | |
/** | |
* The HTTP header containing the tenant id. | |
*/ | |
public static final String TENANT_HEADER = "TENANT_ID"; | |
private final RestTemplate restTemplate; | |
public ConnectorServiceProxyImpl() { | |
this.restTemplate = new RestTemplate(); | |
} | |
@Override | |
public List<FlowDefinitionReference> getAvailableFlows(ConnectorRequestContext context) { | |
final String url = context.getDescription().getUrl() + "/flow/available"; | |
final ResponseEntity<List<FlowDefinitionReference>> responseEntity = restTemplate | |
.exchange( | |
url, | |
HttpMethod.GET, | |
createEntity(context, null), | |
new ParameterizedTypeReference<List<FlowDefinitionReference>>() { | |
} | |
); | |
if (responseEntity.getStatusCode() != HttpStatus.OK) { | |
throw new RemotingException("Error while calling [" + url + "] (" + responseEntity.getStatusCode() + ")."); | |
} | |
return responseEntity.getBody(); | |
} | |
@Override | |
public FlowDefinitionValidationResult getFlowDefinition(String flowReference, ConnectorRequestContext context) { | |
final String url = context.getDescription().getUrl() + "/flow/definition/" + flowReference; | |
final ResponseEntity<FlowDefinitionValidationResult> responseEntity = restTemplate | |
.exchange( | |
url, | |
HttpMethod.GET, | |
createEntity(context, null), | |
new ParameterizedTypeReference<FlowDefinitionValidationResult>() { | |
} | |
); | |
if (!asList(HttpStatus.OK, HttpStatus.CREATED).contains(responseEntity.getStatusCode())) { | |
throw new RemotingException("Error while calling [" + url + "] (" + responseEntity.getStatusCode() + ")."); | |
} | |
return responseEntity.getBody(); | |
} | |
@Override | |
public ValidationResult saveCredentials(Map<String, Object> credentials, ConnectorRequestContext context) { | |
final String url = context.getDescription().getUrl() + "/authentication/credentials/config-entries"; | |
final ResponseEntity<ValidationResult> responseEntity = restTemplate | |
.exchange( | |
url, | |
HttpMethod.POST, | |
createEntity(context, credentials), | |
ValidationResult.class | |
); | |
if (responseEntity.getStatusCode() != HttpStatus.OK) { | |
throw new RemotingException("Error while calling [" + url + "] (" + responseEntity.getStatusCode() + ")."); | |
} | |
return responseEntity.getBody(); | |
} | |
@Override | |
public ValidationResult checkConnectorConfiguredForCurrentUser(ConnectorRequestContext context) { | |
final String url = context.getDescription().getUrl() + "/authentication/credentials/configured"; | |
final ResponseEntity<ValidationResult> responseEntity = restTemplate | |
.exchange( | |
url, | |
HttpMethod.GET, | |
createEntity(context, null), | |
ValidationResult.class | |
); | |
if (responseEntity.getStatusCode() != HttpStatus.OK) { | |
throw new RemotingException("Error while calling [" + url + "] (" + responseEntity.getStatusCode() + ")."); | |
} | |
return responseEntity.getBody(); | |
} | |
/** | |
* Creates the header for the specified content. | |
*/ | |
private <T> HttpEntity<T> createEntity(ConnectorRequestContext context, T content) { | |
final HttpHeaders headers = new HttpHeaders(); | |
final Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); | |
if(!(authentication instanceof EmaAuthenticationToken)){ | |
throw new IllegalStateException("There is no authentication token. Hint: please check the security context."); | |
} | |
final EmaAuthenticationToken token = (EmaAuthenticationToken) authentication; | |
headers.set(ACCESS_KEY_HEADER, context.getAccessKey()); | |
headers.set(USER_NAME_HEADER, token.getUser().getUsername()); | |
headers.set(TENANT_HEADER, token.getBaseTenantId()); | |
headers.set(COOKIE, "SESSION=" + token.getSessionId()); | |
return new HttpEntity<>(content, headers); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment