Search in sources :

Example 96 with LinkedMultiValueMap

use of org.springframework.util.LinkedMultiValueMap in project profile by craftercms.

the class ProfileServiceRestClient method addProfileAttachment.

@Override
public ProfileAttachment addProfileAttachment(String profileId, String attachmentName, InputStream file) throws ProfileException {
    MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
    params.add(PARAM_ACCESS_TOKEN_ID, accessTokenIdResolver.getAccessTokenId());
    params.add(PARAM_FILENAME, attachmentName);
    String url = getAbsoluteUrl(BASE_URL_PROFILE + URL_PROFILE_UPLOAD_ATTACHMENT);
    File tmpFile = null;
    try {
        tmpFile = File.createTempFile(profileId + "-" + attachmentName, "attachment");
        FileUtils.copyInputStreamToFile(file, tmpFile);
        params.add("attachment", new FileSystemResource(tmpFile));
        return doPostForUpload(url, params, ProfileAttachment.class, profileId);
    } catch (IOException e) {
        throw new I10nProfileException(ERROR_KEY_TMP_COPY_FAILED, e, attachmentName, profileId);
    } finally {
        try {
            file.close();
            FileUtils.forceDelete(tmpFile);
        } catch (Throwable e) {
        }
    }
}
Also used : I10nProfileException(org.craftercms.profile.api.exceptions.I10nProfileException) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) FileSystemResource(org.springframework.core.io.FileSystemResource) IOException(java.io.IOException) File(java.io.File)

Example 97 with LinkedMultiValueMap

use of org.springframework.util.LinkedMultiValueMap in project seldon-core by SeldonIO.

the class InternalPredictionService method queryREST.

private SeldonMessage queryREST(String path, String dataString, PredictiveUnitState state, Endpoint endpoint, boolean isDefault) {
    long timeNow = System.currentTimeMillis();
    URI uri;
    try {
        URIBuilder builder = new URIBuilder().setScheme("http").setHost(endpoint.getServiceHost()).setPort(endpoint.getServicePort()).setPath("/" + path);
        uri = builder.build();
    } catch (URISyntaxException e) {
        throw new APIException(APIException.ApiExceptionType.ENGINE_INVALID_ENDPOINT_URL, "Host: " + endpoint.getServiceHost() + " port:" + endpoint.getServicePort());
    }
    try {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        headers.add(MODEL_NAME_HEADER, state.name);
        headers.add(MODEL_IMAGE_HEADER, state.imageName);
        headers.add(MODEL_VERSION_HEADER, state.imageVersion);
        MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
        map.add("json", dataString);
        map.add("isDefault", Boolean.toString(isDefault));
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);
        logger.info("Requesting " + uri.toString());
        ResponseEntity<String> httpResponse = restTemplate.postForEntity(uri, request, String.class);
        try {
            if (httpResponse.getStatusCode().is2xxSuccessful()) {
                SeldonMessage.Builder builder = SeldonMessage.newBuilder();
                String response = httpResponse.getBody();
                logger.info(response);
                JsonFormat.parser().ignoringUnknownFields().merge(response, builder);
                return builder.build();
            } else {
                logger.error("Couldn't retrieve prediction from external prediction server -- bad http return code: " + httpResponse.getStatusCode());
                throw new APIException(APIException.ApiExceptionType.ENGINE_MICROSERVICE_ERROR, String.format("Bad return code %d", httpResponse.getStatusCode()));
            }
        } finally {
            if (logger.isDebugEnabled())
                logger.debug("External prediction server took " + (System.currentTimeMillis() - timeNow) + "ms");
        }
    } catch (IOException e) {
        logger.error("Couldn't retrieve prediction from external prediction server - ", e);
        throw new APIException(APIException.ApiExceptionType.ENGINE_MICROSERVICE_ERROR, e.toString());
    } catch (Exception e) {
        logger.error("Couldn't retrieve prediction from external prediction server - ", e);
        throw new APIException(APIException.ApiExceptionType.ENGINE_MICROSERVICE_ERROR, e.toString());
    } finally {
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) APIException(io.seldon.engine.exception.APIException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) IOException(java.io.IOException) URIBuilder(org.apache.http.client.utils.URIBuilder) APIException(io.seldon.engine.exception.APIException) SeldonMessage(io.seldon.protos.PredictionProtos.SeldonMessage) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap)

Example 98 with LinkedMultiValueMap

use of org.springframework.util.LinkedMultiValueMap in project spring-boot-admin by codecentric.

the class JdkPerInstanceCookieStoreTest method cookies_should_be_fetched_and_converted_from_store.

@Test
void cookies_should_be_fetched_and_converted_from_store() throws IOException {
    MultiValueMap<String, String> storeMap = new LinkedMultiValueMap<>();
    storeMap.add("Cookie", "name=value");
    storeMap.add("Cookie", "name2=tricky=value");
    final URI uri = URI.create("http://localhost/test");
    when(cookieHandler.get(eq(uri), any())).thenReturn(storeMap);
    final MultiValueMap<String, String> cookieMap = store.get(INSTANCE_ID, uri, new LinkedMultiValueMap<>());
    assertThat(cookieMap).containsEntry("name", singletonList("value"));
    assertThat(cookieMap).containsEntry("name2", singletonList("tricky=value"));
}
Also used : LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Example 99 with LinkedMultiValueMap

use of org.springframework.util.LinkedMultiValueMap in project spring-security by spring-projects.

the class OAuth2ClientBeanDefinitionParserTests method requestWhenAuthorizationResponseMatchThenProcess.

@Test
public void requestWhenAuthorizationResponseMatchThenProcess() throws Exception {
    this.spring.configLocations(xml("CustomConfiguration")).autowire();
    ClientRegistration clientRegistration = this.clientRegistrationRepository.findByRegistrationId("google");
    OAuth2AuthorizationRequest authorizationRequest = createAuthorizationRequest(clientRegistration);
    given(this.authorizationRequestRepository.loadAuthorizationRequest(any())).willReturn(authorizationRequest);
    given(this.authorizationRequestRepository.removeAuthorizationRequest(any(), any())).willReturn(authorizationRequest);
    OAuth2AccessTokenResponse accessTokenResponse = TestOAuth2AccessTokenResponses.accessTokenResponse().build();
    given(this.accessTokenResponseClient.getTokenResponse(any())).willReturn(accessTokenResponse);
    MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
    params.add("code", "code123");
    params.add("state", authorizationRequest.getState());
    // @formatter:off
    this.mvc.perform(get(authorizationRequest.getRedirectUri()).params(params)).andExpect(status().is3xxRedirection()).andExpect(redirectedUrl(authorizationRequest.getRedirectUri()));
    // @formatter:on
    ArgumentCaptor<OAuth2AuthorizedClient> authorizedClientCaptor = ArgumentCaptor.forClass(OAuth2AuthorizedClient.class);
    verify(this.authorizedClientRepository).saveAuthorizedClient(authorizedClientCaptor.capture(), any(), any(), any());
    OAuth2AuthorizedClient authorizedClient = authorizedClientCaptor.getValue();
    assertThat(authorizedClient.getClientRegistration()).isEqualTo(clientRegistration);
    assertThat(authorizedClient.getAccessToken()).isEqualTo(accessTokenResponse.getAccessToken());
}
Also used : OAuth2AccessTokenResponse(org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse) ClientRegistration(org.springframework.security.oauth2.client.registration.ClientRegistration) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) RegisteredOAuth2AuthorizedClient(org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient) OAuth2AuthorizedClient(org.springframework.security.oauth2.client.OAuth2AuthorizedClient) OAuth2AuthorizationRequest(org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest) Test(org.junit.jupiter.api.Test)

Example 100 with LinkedMultiValueMap

use of org.springframework.util.LinkedMultiValueMap in project spring-security by spring-projects.

the class JwtBearerGrantRequestEntityConverter method createParameters.

@Override
protected MultiValueMap<String, String> createParameters(JwtBearerGrantRequest jwtBearerGrantRequest) {
    ClientRegistration clientRegistration = jwtBearerGrantRequest.getClientRegistration();
    MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>();
    parameters.add(OAuth2ParameterNames.GRANT_TYPE, jwtBearerGrantRequest.getGrantType().getValue());
    parameters.add(OAuth2ParameterNames.ASSERTION, jwtBearerGrantRequest.getJwt().getTokenValue());
    if (!CollectionUtils.isEmpty(clientRegistration.getScopes())) {
        parameters.add(OAuth2ParameterNames.SCOPE, StringUtils.collectionToDelimitedString(clientRegistration.getScopes(), " "));
    }
    if (ClientAuthenticationMethod.CLIENT_SECRET_POST.equals(clientRegistration.getClientAuthenticationMethod()) || ClientAuthenticationMethod.POST.equals(clientRegistration.getClientAuthenticationMethod())) {
        parameters.add(OAuth2ParameterNames.CLIENT_ID, clientRegistration.getClientId());
        parameters.add(OAuth2ParameterNames.CLIENT_SECRET, clientRegistration.getClientSecret());
    }
    return parameters;
}
Also used : ClientRegistration(org.springframework.security.oauth2.client.registration.ClientRegistration) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap)

Aggregations

LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)427 Test (org.junit.Test)159 HttpHeaders (org.springframework.http.HttpHeaders)135 MultiValueMap (org.springframework.util.MultiValueMap)102 Test (org.junit.jupiter.api.Test)88 HttpEntity (org.springframework.http.HttpEntity)70 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)45 List (java.util.List)44 HashMap (java.util.HashMap)38 GuardedString (eu.bcvsolutions.idm.core.security.api.domain.GuardedString)37 MediaType (org.springframework.http.MediaType)35 URI (java.net.URI)34 IdmIdentityDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)33 Map (java.util.Map)32 AbstractReadWriteDtoControllerRestTest (eu.bcvsolutions.idm.core.api.rest.AbstractReadWriteDtoControllerRestTest)30 ArrayList (java.util.ArrayList)29 IOException (java.io.IOException)27 UUID (java.util.UUID)27 lombok.val (lombok.val)27 Autowired (org.springframework.beans.factory.annotation.Autowired)26