Search in sources :

Example 91 with UriComponents

use of org.springframework.web.util.UriComponents in project spring-data-commons by spring-projects.

the class HateoasSortHandlerMethodArgumentResolverUnitTests method returnCorrectTemplateVariables.

// DATACMNS-418
@Test
public void returnCorrectTemplateVariables() {
    UriComponents uriComponents = UriComponentsBuilder.fromPath("/").build();
    HateoasSortHandlerMethodArgumentResolver resolver = new HateoasSortHandlerMethodArgumentResolver();
    assertThat(resolver.getSortTemplateVariables(null, uriComponents).toString()).isEqualTo("{?sort}");
}
Also used : UriComponents(org.springframework.web.util.UriComponents) Test(org.junit.Test)

Example 92 with UriComponents

use of org.springframework.web.util.UriComponents in project connectors-workspace-one by vmware.

the class AwsCertController method validateUrl.

private boolean validateUrl(String approvalUrl) {
    boolean isValid;
    try {
        UriComponents uriComponents = UriComponentsBuilder.fromHttpUrl(approvalUrl).build();
        isValid = verifyHost(uriComponents) && verifyPath(uriComponents);
    } catch (IllegalArgumentException e) {
        isValid = false;
    }
    if (!isValid) {
        logger.warn("Approval url was not valid: {}", approvalUrl);
    }
    return isValid;
}
Also used : UriComponents(org.springframework.web.util.UriComponents)

Example 93 with UriComponents

use of org.springframework.web.util.UriComponents in project molgenis by molgenis.

the class RestService method convertFile.

private FileMeta convertFile(Attribute attr, Object paramValue, Object entityId) {
    FileMeta value;
    if (paramValue != null) {
        /*
			 * If an entity is updated and no new file is passed, use the old file value
			 */
        if (!(paramValue instanceof MultipartFile)) {
            EntityType entityType = attr.getEntity();
            Attribute idAttribute = entityType.getIdAttribute();
            Object idValue = this.toEntityValue(idAttribute, entityId, null);
            Entity oldEntity = dataService.findOneById(entityType.getId(), idValue);
            if (paramValue instanceof String) {
                FileMeta entity = (FileMeta) oldEntity.getEntity(attr.getName());
                if (entity.get(FILENAME).equals(paramValue)) {
                    value = entity;
                } else {
                    throw new MolgenisDataException("Cannot update entity with file attribute without passing file," + " while changing the name of the existing file attribute");
                }
            } else {
                throw new MolgenisDataException(format("Attribute [%s] value is of type [%s] instead of [%s]", attr.getName(), paramValue.getClass().getSimpleName(), MultipartFile.class.getSimpleName()));
            }
        } else {
            MultipartFile multipartFile = (MultipartFile) paramValue;
            String id = idGenerator.generateId();
            try {
                fileStore.store(multipartFile.getInputStream(), id);
            } catch (IOException e) {
                throw new MolgenisDataException(e);
            }
            FileMeta fileEntity = fileMetaFactory.create(id);
            fileEntity.setFilename(multipartFile.getOriginalFilename());
            fileEntity.setContentType(multipartFile.getContentType());
            fileEntity.setSize(multipartFile.getSize());
            ServletUriComponentsBuilder currentRequest = servletUriComponentsBuilderFactory.fromCurrentRequest();
            UriComponents downloadUri = currentRequest.replacePath(FileDownloadController.URI + '/' + id).replaceQuery(null).build();
            fileEntity.setUrl(downloadUri.toUriString());
            dataService.add(FILE_META, fileEntity);
            value = fileEntity;
        }
    } else {
        value = null;
    }
    return value;
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) Entity(org.molgenis.data.Entity) MultipartFile(org.springframework.web.multipart.MultipartFile) MolgenisDataException(org.molgenis.data.MolgenisDataException) UriComponents(org.springframework.web.util.UriComponents) ServletUriComponentsBuilder(org.springframework.web.servlet.support.ServletUriComponentsBuilder) Attribute(org.molgenis.data.meta.model.Attribute) IOException(java.io.IOException) FileMeta(org.molgenis.data.file.model.FileMeta)

Example 94 with UriComponents

use of org.springframework.web.util.UriComponents in project molgenis by molgenis.

the class SwaggerController method init.

/**
 * Serves the Swagger UI which allows you to try out the documented endpoints.
 * Sets the url parameter to the swagger yaml that describes the REST API.
 * Creates an apiKey token for the current user.
 */
@GetMapping
public String init(Model model) {
    final UriComponents uriComponents = ServletUriComponentsBuilder.fromCurrentContextPath().build();
    model.addAttribute("molgenisUrl", uriComponents.toUriString() + URI + "/swagger.yml");
    model.addAttribute("baseUrl", uriComponents.toUriString());
    final String currentUsername = SecurityUtils.getCurrentUsername();
    if (currentUsername != null) {
        model.addAttribute("token", tokenService.generateAndStoreToken(currentUsername, "For Swagger UI"));
    }
    return "view-swagger-ui";
}
Also used : UriComponents(org.springframework.web.util.UriComponents) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 95 with UriComponents

use of org.springframework.web.util.UriComponents in project microservices by pwillhan.

the class PersonResourceProcessor method process.

@Override
public Resource<Person> process(Resource<Person> resource) {
    String id = Long.toString(resource.getContent().getId());
    UriComponents uriComponents = ServletUriComponentsBuilder.fromCurrentContextPath().path("/people/{id}/photo").buildAndExpand(id);
    String uri = uriComponents.toUriString();
    resource.add(new Link(uri, "photo"));
    return resource;
}
Also used : UriComponents(org.springframework.web.util.UriComponents) Link(org.springframework.hateoas.Link)

Aggregations

UriComponents (org.springframework.web.util.UriComponents)133 Test (org.junit.jupiter.api.Test)43 Test (org.junit.Test)37 UriComponentsBuilder (org.springframework.web.util.UriComponentsBuilder)26 MvcUriComponentsBuilder (org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder)7 URI (java.net.URI)5 HashMap (java.util.HashMap)5 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)4 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 MvcResult (org.springframework.test.web.servlet.MvcResult)4 ServletUriComponentsBuilder (org.springframework.web.servlet.support.ServletUriComponentsBuilder)4 MethodParameter (org.springframework.core.MethodParameter)3 ServletServerHttpRequest (org.springframework.http.server.ServletServerHttpRequest)3 GetMapping (org.springframework.web.bind.annotation.GetMapping)3 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)3 ArrayList (java.util.ArrayList)2 LinkedHashSet (java.util.LinkedHashSet)2 List (java.util.List)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 Matchers.anyString (org.mockito.Matchers.anyString)2