Search in sources :

Example 21 with RegisteredServiceJsonSerializer

use of org.apereo.cas.services.util.RegisteredServiceJsonSerializer in project cas by apereo.

the class RegisteredServicesEndpoint method export.

/**
 * Export.
 *
 * @return the response entity
 */
@GetMapping(path = "/export", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
@ResponseBody
@Operation(summary = "Export registered services as a zip file")
public ResponseEntity<Resource> export() {
    val serializer = new RegisteredServiceJsonSerializer();
    val resource = CompressionUtils.toZipFile(servicesManager.stream(), Unchecked.function(entry -> {
        val service = (RegisteredService) entry;
        val fileName = String.format("%s-%s", service.getName(), service.getId());
        val sourceFile = File.createTempFile(fileName, ".json");
        serializer.to(sourceFile, service);
        return sourceFile;
    }), "services");
    val headers = new HttpHeaders();
    headers.setContentDisposition(ContentDisposition.attachment().filename(Objects.requireNonNull(resource.getFilename())).build());
    return new ResponseEntity<>(resource, headers, HttpStatus.OK);
}
Also used : lombok.val(lombok.val) CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) PathVariable(org.springframework.web.bind.annotation.PathVariable) ActuatorMediaType(org.springframework.boot.actuate.endpoint.http.ActuatorMediaType) ZipInputStream(java.util.zip.ZipInputStream) RegisteredServiceJsonSerializer(org.apereo.cas.services.util.RegisteredServiceJsonSerializer) StringUtils(org.apache.commons.lang3.StringUtils) WebApplicationService(org.apereo.cas.authentication.principal.WebApplicationService) Operation(io.swagger.v3.oas.annotations.Operation) HttpServletRequest(javax.servlet.http.HttpServletRequest) ContentDisposition(org.springframework.http.ContentDisposition) ByteArrayInputStream(java.io.ByteArrayInputStream) StringSerializer(org.apereo.cas.util.serialization.StringSerializer) CollectionUtils(org.apereo.cas.util.CollectionUtils) GetMapping(org.springframework.web.bind.annotation.GetMapping) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping) ServiceFactory(org.apereo.cas.authentication.principal.ServiceFactory) ServicesManager(org.apereo.cas.services.ServicesManager) Resource(org.springframework.core.io.Resource) PostMapping(org.springframework.web.bind.annotation.PostMapping) Unchecked(org.jooq.lambda.Unchecked) HttpHeaders(org.springframework.http.HttpHeaders) MediaType(org.springframework.http.MediaType) Collection(java.util.Collection) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) lombok.val(lombok.val) CompressionUtils(org.apereo.cas.util.CompressionUtils) IOException(java.io.IOException) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) RegisteredService(org.apereo.cas.services.RegisteredService) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) BaseCasActuatorEndpoint(org.apereo.cas.web.BaseCasActuatorEndpoint) RestControllerEndpoint(org.springframework.boot.actuate.endpoint.web.annotation.RestControllerEndpoint) Objects(java.util.Objects) IOUtils(org.apache.commons.io.IOUtils) HttpStatus(org.springframework.http.HttpStatus) Slf4j(lombok.extern.slf4j.Slf4j) Stream(java.util.stream.Stream) JacksonObjectMapperFactory(org.apereo.cas.util.serialization.JacksonObjectMapperFactory) NumberUtils(org.apache.commons.lang3.math.NumberUtils) ResponseEntity(org.springframework.http.ResponseEntity) HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) RegisteredServiceJsonSerializer(org.apereo.cas.services.util.RegisteredServiceJsonSerializer) GetMapping(org.springframework.web.bind.annotation.GetMapping) Operation(io.swagger.v3.oas.annotations.Operation) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 22 with RegisteredServiceJsonSerializer

use of org.apereo.cas.services.util.RegisteredServiceJsonSerializer in project cas by apereo.

the class RegisteredServicesEndpointTests method verifyBulkImportAsZip.

@Test
public void verifyBulkImportAsZip() throws Exception {
    val request = new MockHttpServletRequest();
    request.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
    try (val out = new ByteArrayOutputStream(2048);
        val zipStream = new ZipOutputStream(out)) {
        var registeredService = RegisteredServiceTestUtils.getRegisteredService();
        val content = new RegisteredServiceJsonSerializer().toString(registeredService);
        var name = registeredService.getName() + ".json";
        val e = new ZipEntry(name);
        zipStream.putNextEntry(e);
        val data = content.getBytes(StandardCharsets.UTF_8);
        zipStream.write(data, 0, data.length);
        zipStream.closeEntry();
        request.setContent(out.toByteArray());
    }
    assertEquals(HttpStatus.OK, endpoint.importService(request).getStatusCode());
}
Also used : lombok.val(lombok.val) RegisteredServiceJsonSerializer(org.apereo.cas.services.util.RegisteredServiceJsonSerializer) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ZipOutputStream(java.util.zip.ZipOutputStream) ZipEntry(java.util.zip.ZipEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test)

Example 23 with RegisteredServiceJsonSerializer

use of org.apereo.cas.services.util.RegisteredServiceJsonSerializer in project cas by apereo.

the class RegisteredServicesEndpointTests method verifyImportOperationAsJson.

@Test
public void verifyImportOperationAsJson() throws Exception {
    val request = new MockHttpServletRequest();
    val content = new RegisteredServiceJsonSerializer().toString(RegisteredServiceTestUtils.getRegisteredService());
    request.setContent(content.getBytes(StandardCharsets.UTF_8));
    assertEquals(HttpStatus.CREATED, endpoint.importService(request).getStatusCode());
}
Also used : lombok.val(lombok.val) RegisteredServiceJsonSerializer(org.apereo.cas.services.util.RegisteredServiceJsonSerializer) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Test(org.junit.jupiter.api.Test)

Example 24 with RegisteredServiceJsonSerializer

use of org.apereo.cas.services.util.RegisteredServiceJsonSerializer in project cas by apereo.

the class RegisteredServiceResourceTests method runTest.

private void runTest(final String attrName, final String attrValue, final String credentials, final ResultMatcher result) throws Exception {
    val registeredServiceResource = getRegisteredServiceResource(attrName, attrValue);
    val service = RegisteredServiceTestUtils.getRegisteredService();
    val sz = new RegisteredServiceJsonSerializer();
    try (val writer = new StringWriter()) {
        sz.to(writer, service);
        configureMockMvcFor(registeredServiceResource).perform(post("/cas/v1/services").contentType(MediaType.APPLICATION_JSON).header("Authorization", "Basic " + EncodingUtils.encodeBase64(credentials)).content(writer.toString())).andExpect(result);
    }
}
Also used : lombok.val(lombok.val) RegisteredServiceJsonSerializer(org.apereo.cas.services.util.RegisteredServiceJsonSerializer) StringWriter(java.io.StringWriter)

Example 25 with RegisteredServiceJsonSerializer

use of org.apereo.cas.services.util.RegisteredServiceJsonSerializer in project cas by apereo.

the class GitServiceRegistryTests method verifyPullFails.

@Test
public void verifyPullFails() throws Exception {
    val gitRepository = mock(GitRepository.class);
    when(gitRepository.getObjectsInRepository()).thenThrow(new JGitInternalException("error"));
    when(gitRepository.getObjectsInRepository(any())).thenThrow(new JGitInternalException("error"));
    when(gitRepository.getRepositoryDirectory()).thenReturn(gitRepositoryInstance.getRepositoryDirectory());
    val svc = buildRegisteredServiceInstance(RandomUtils.nextLong(), RegexRegisteredService.class);
    svc.setId(RegisteredService.INITIAL_IDENTIFIER_VALUE);
    newServiceRegistry.save(svc);
    val size = newServiceRegistry.load().size();
    val registry = new GitServiceRegistry(applicationContext, gitRepository, CollectionUtils.wrapList(new RegisteredServiceJsonSerializer(), new RegisteredServiceYamlSerializer()), false, null, List.of(), List.of());
    assertEquals(size, registry.load().size());
}
Also used : lombok.val(lombok.val) RegisteredServiceJsonSerializer(org.apereo.cas.services.util.RegisteredServiceJsonSerializer) RegisteredServiceYamlSerializer(org.apereo.cas.services.util.RegisteredServiceYamlSerializer) JGitInternalException(org.eclipse.jgit.api.errors.JGitInternalException) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

lombok.val (lombok.val)31 RegisteredServiceJsonSerializer (org.apereo.cas.services.util.RegisteredServiceJsonSerializer)31 Test (org.junit.jupiter.api.Test)23 ClassPathResource (org.springframework.core.io.ClassPathResource)8 File (java.io.File)6 ChainingAttributeReleasePolicy (org.apereo.cas.services.ChainingAttributeReleasePolicy)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)5 RegisteredServiceYamlSerializer (org.apereo.cas.services.util.RegisteredServiceYamlSerializer)4 StringWriter (java.io.StringWriter)2 ArrayList (java.util.ArrayList)2 KafkaObjectFactory (org.apereo.cas.kafka.KafkaObjectFactory)2 RegisteredService (org.apereo.cas.services.RegisteredService)2 CasRegisteredServiceDeletedEvent (org.apereo.cas.support.events.service.CasRegisteredServiceDeletedEvent)2 CasRegisteredServiceSavedEvent (org.apereo.cas.support.events.service.CasRegisteredServiceSavedEvent)2 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)2 RefreshScope (org.springframework.cloud.context.config.annotation.RefreshScope)2 Bean (org.springframework.context.annotation.Bean)2 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1