Search in sources :

Example 1 with NamespaceJson

use of org.eclipse.openvsx.json.NamespaceJson in project openvsx by eclipse.

the class LocalRegistryService method getNamespace.

@Override
public NamespaceJson getNamespace(String namespaceName) {
    var namespace = repositories.findNamespace(namespaceName);
    if (namespace == null)
        throw new NotFoundException();
    var json = new NamespaceJson();
    json.name = namespace.getName();
    json.extensions = new LinkedHashMap<>();
    var serverUrl = UrlUtil.getBaseUrl();
    for (var ext : repositories.findActiveExtensions(namespace)) {
        String url = createApiUrl(serverUrl, "api", namespace.getName(), ext.getName());
        json.extensions.put(ext.getName(), url);
    }
    json.verified = repositories.countMemberships(namespace, NamespaceMembership.ROLE_OWNER) > 0;
    json.access = "restricted";
    return json;
}
Also used : NamespaceJson(org.eclipse.openvsx.json.NamespaceJson) NotFoundException(org.eclipse.openvsx.util.NotFoundException)

Example 2 with NamespaceJson

use of org.eclipse.openvsx.json.NamespaceJson in project openvsx by eclipse.

the class AdminAPITest method testCreateNamespace.

@Test
public void testCreateNamespace() throws Exception {
    mockAdminUser();
    mockMvc.perform(post("/admin/create-namespace").contentType(MediaType.APPLICATION_JSON).content(namespaceJson(n -> {
        n.name = "foobar";
    })).with(user("admin_user").authorities(new SimpleGrantedAuthority(("ROLE_ADMIN")))).with(csrf().asHeader())).andExpect(status().isCreated()).andExpect(redirectedUrl("http://localhost/admin/namespace/foobar")).andExpect(content().json(successJson("Created namespace foobar")));
}
Also used : org.eclipse.openvsx.entities(org.eclipse.openvsx.entities) UserJson(org.eclipse.openvsx.json.UserJson) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) VSCodeIdService(org.eclipse.openvsx.adapter.VSCodeIdService) Autowired(org.springframework.beans.factory.annotation.Autowired) TestConfiguration(org.springframework.boot.test.context.TestConfiguration) AzureBlobStorageService(org.eclipse.openvsx.storage.AzureBlobStorageService) LockProvider(net.javacrumbs.shedlock.core.LockProvider) ExtensionJson(org.eclipse.openvsx.json.ExtensionJson) MockBean(org.springframework.boot.test.mock.mockito.MockBean) SearchUtilService(org.eclipse.openvsx.search.SearchUtilService) SpyBean(org.springframework.boot.test.mock.mockito.SpyBean) MediaType(org.springframework.http.MediaType) MockMvcResultMatchers.redirectedUrl(org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl) TokenService(org.eclipse.openvsx.security.TokenService) Test(org.junit.jupiter.api.Test) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest) Streamable(org.springframework.data.util.Streamable) SecurityMockMvcRequestPostProcessors.csrf(org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf) UserPublishInfoJson(org.eclipse.openvsx.json.UserPublishInfoJson) AzureDownloadCountService(org.eclipse.openvsx.storage.AzureDownloadCountService) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) java.util(java.util) LocalDateTime(java.time.LocalDateTime) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) NamespaceMembershipListJson(org.eclipse.openvsx.json.NamespaceMembershipListJson) MockMvcResultMatchers.content(org.springframework.test.web.servlet.result.MockMvcResultMatchers.content) MockMvc(org.springframework.test.web.servlet.MockMvc) MockMvcResultMatchers.status(org.springframework.test.web.servlet.result.MockMvcResultMatchers.status) MockMvcRequestBuilders.post(org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post) NamespaceJson(org.eclipse.openvsx.json.NamespaceJson) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) AutoConfigureWebClient(org.springframework.boot.test.autoconfigure.web.client.AutoConfigureWebClient) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) EntityManager(javax.persistence.EntityManager) OAuth2UserServices(org.eclipse.openvsx.security.OAuth2UserServices) GoogleCloudStorageService(org.eclipse.openvsx.storage.GoogleCloudStorageService) SecurityMockMvcRequestPostProcessors.user(org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user) ResultJson(org.eclipse.openvsx.json.ResultJson) Consumer(java.util.function.Consumer) RepositoryService(org.eclipse.openvsx.repositories.RepositoryService) Mockito(org.mockito.Mockito) NamespaceMembershipJson(org.eclipse.openvsx.json.NamespaceMembershipJson) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) MockMvcRequestBuilders.get(org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get) Bean(org.springframework.context.annotation.Bean) EclipseService(org.eclipse.openvsx.eclipse.EclipseService) StorageUtilService(org.eclipse.openvsx.storage.StorageUtilService) ClientRegistrationRepository(org.springframework.security.oauth2.client.registration.ClientRegistrationRepository) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) Test(org.junit.jupiter.api.Test) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)

Example 3 with NamespaceJson

use of org.eclipse.openvsx.json.NamespaceJson in project openvsx by eclipse.

the class UserAPI method getOwnNamespaces.

@GetMapping(path = "/user/namespaces", produces = MediaType.APPLICATION_JSON_VALUE)
public List<NamespaceJson> getOwnNamespaces() {
    var user = users.findLoggedInUser();
    if (user == null) {
        throw new ResponseStatusException(HttpStatus.FORBIDDEN);
    }
    var memberships = repositories.findMemberships(user, NamespaceMembership.ROLE_OWNER);
    return memberships.map(membership -> {
        var namespace = membership.getNamespace();
        var json = new NamespaceJson();
        json.name = namespace.getName();
        json.extensions = new LinkedHashMap<>();
        var serverUrl = UrlUtil.getBaseUrl();
        for (var ext : repositories.findActiveExtensions(namespace)) {
            String url = createApiUrl(serverUrl, "api", namespace.getName(), ext.getName());
            json.extensions.put(ext.getName(), url);
        }
        json.verified = true;
        json.membersUrl = createApiUrl(serverUrl, "user", "namespace", namespace.getName(), "members");
        json.roleUrl = createApiUrl(serverUrl, "user", "namespace", namespace.getName(), "role");
        return json;
    }).toList();
}
Also used : UserJson(org.eclipse.openvsx.json.UserJson) PathVariable(org.springframework.web.bind.annotation.PathVariable) RequestParam(org.springframework.web.bind.annotation.RequestParam) CodedAuthException(org.eclipse.openvsx.security.CodedAuthException) ErrorResultException(org.eclipse.openvsx.util.ErrorResultException) Autowired(org.springframework.beans.factory.annotation.Autowired) NamespaceMembershipListJson(org.eclipse.openvsx.json.NamespaceMembershipListJson) UrlUtil(org.eclipse.openvsx.util.UrlUtil) WebAttributes(org.springframework.security.web.WebAttributes) TimeUtil(org.eclipse.openvsx.util.TimeUtil) ModelMap(org.springframework.ui.ModelMap) LinkedHashMap(java.util.LinkedHashMap) AccessTokenJson(org.eclipse.openvsx.json.AccessTokenJson) ErrorJson(org.eclipse.openvsx.json.ErrorJson) HttpServletRequest(javax.servlet.http.HttpServletRequest) PersonalAccessToken(org.eclipse.openvsx.entities.PersonalAccessToken) NamespaceJson(org.eclipse.openvsx.json.NamespaceJson) GetMapping(org.springframework.web.bind.annotation.GetMapping) AuthenticationException(org.springframework.security.core.AuthenticationException) CsrfTokenJson(org.eclipse.openvsx.json.CsrfTokenJson) UrlUtil.createApiUrl(org.eclipse.openvsx.util.UrlUtil.createApiUrl) PostMapping(org.springframework.web.bind.annotation.PostMapping) ResponseStatusException(org.springframework.web.server.ResponseStatusException) Transactional(javax.transaction.Transactional) MediaType(org.springframework.http.MediaType) EntityManager(javax.persistence.EntityManager) RestController(org.springframework.web.bind.annotation.RestController) ResultJson(org.eclipse.openvsx.json.ResultJson) RepositoryService(org.eclipse.openvsx.repositories.RepositoryService) HttpStatus(org.springframework.http.HttpStatus) ModelAndView(org.springframework.web.servlet.ModelAndView) List(java.util.List) CollectionUtil(org.eclipse.openvsx.util.CollectionUtil) ResponseEntity(org.springframework.http.ResponseEntity) EclipseService(org.eclipse.openvsx.eclipse.EclipseService) CsrfToken(org.springframework.security.web.csrf.CsrfToken) NamespaceMembership(org.eclipse.openvsx.entities.NamespaceMembership) NamespaceJson(org.eclipse.openvsx.json.NamespaceJson) ResponseStatusException(org.springframework.web.server.ResponseStatusException) LinkedHashMap(java.util.LinkedHashMap) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 4 with NamespaceJson

use of org.eclipse.openvsx.json.NamespaceJson in project openvsx by eclipse.

the class RegistryAPITest method namespaceJson.

private String namespaceJson(Consumer<NamespaceJson> content) throws JsonProcessingException {
    var json = new NamespaceJson();
    content.accept(json);
    return new ObjectMapper().writeValueAsString(json);
}
Also used : NamespaceJson(org.eclipse.openvsx.json.NamespaceJson) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 5 with NamespaceJson

use of org.eclipse.openvsx.json.NamespaceJson in project openvsx by eclipse.

the class AdminAPITest method namespaceJson.

private String namespaceJson(Consumer<NamespaceJson> content) throws JsonProcessingException {
    var json = new NamespaceJson();
    content.accept(json);
    return new ObjectMapper().writeValueAsString(json);
}
Also used : NamespaceJson(org.eclipse.openvsx.json.NamespaceJson) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

NamespaceJson (org.eclipse.openvsx.json.NamespaceJson)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 EntityManager (javax.persistence.EntityManager)2 EclipseService (org.eclipse.openvsx.eclipse.EclipseService)2 NamespaceMembershipListJson (org.eclipse.openvsx.json.NamespaceMembershipListJson)2 ResultJson (org.eclipse.openvsx.json.ResultJson)2 UserJson (org.eclipse.openvsx.json.UserJson)2 RepositoryService (org.eclipse.openvsx.repositories.RepositoryService)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 MediaType (org.springframework.http.MediaType)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 LocalDateTime (java.time.LocalDateTime)1 java.util (java.util)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Consumer (java.util.function.Consumer)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 Transactional (javax.transaction.Transactional)1 LockProvider (net.javacrumbs.shedlock.core.LockProvider)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1