Changeset 1870


Ignore:
Timestamp:
04/10/12 13:46:22 (12 years ago)
Author:
twagoo
Message:

If requested component not found, return 404 in REST service

Location:
ComponentRegistry/trunk/ComponentRegistry/src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/impl/database/ComponentRegistryDbImpl.java

    r1869 r1870  
    645645        return new CMDComponentSpecExpanderDbImpl(this);
    646646    }
     647
     648    @Override
     649    public String toString() {
     650        return getName();
     651    }
    647652}
  • ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/ComponentRegistryRestService.java

    r1863 r1870  
    11package clarin.cmdi.componentregistry.rest;
    22
     3import clarin.cmdi.componentregistry.ComponentRegistry;
     4import clarin.cmdi.componentregistry.ComponentRegistryException;
     5import clarin.cmdi.componentregistry.ComponentRegistryFactory;
     6import clarin.cmdi.componentregistry.DeleteFailedException;
     7import clarin.cmdi.componentregistry.UserCredentials;
     8import clarin.cmdi.componentregistry.UserUnauthorizedException;
     9import clarin.cmdi.componentregistry.components.CMDComponentSpec;
     10import clarin.cmdi.componentregistry.model.AbstractDescription;
     11import clarin.cmdi.componentregistry.model.Comment;
     12import clarin.cmdi.componentregistry.model.CommentResponse;
     13import clarin.cmdi.componentregistry.model.ComponentDescription;
     14import clarin.cmdi.componentregistry.model.ProfileDescription;
     15import clarin.cmdi.componentregistry.model.RegisterResponse;
     16import com.sun.jersey.multipart.FormDataParam;
     17import com.sun.jersey.spi.inject.Inject;
    318import java.io.IOException;
    419import java.io.InputStream;
     
    924import java.util.Arrays;
    1025import java.util.List;
    11 
    1226import javax.servlet.http.HttpServletRequest;
    1327import javax.ws.rs.Consumes;
     
    2539import javax.ws.rs.core.MediaType;
    2640import javax.ws.rs.core.Response;
     41import javax.ws.rs.core.Response.Status;
    2742import javax.ws.rs.core.SecurityContext;
    2843import javax.ws.rs.core.StreamingOutput;
    2944import javax.ws.rs.core.UriInfo;
    30 import javax.ws.rs.core.Response.Status;
    31 
    3245import org.slf4j.Logger;
    3346import org.slf4j.LoggerFactory;
    34 
    35 import clarin.cmdi.componentregistry.ComponentRegistry;
    36 import clarin.cmdi.componentregistry.ComponentRegistryException;
    37 import clarin.cmdi.componentregistry.ComponentRegistryFactory;
    38 import clarin.cmdi.componentregistry.DeleteFailedException;
    39 import clarin.cmdi.componentregistry.UserCredentials;
    40 import clarin.cmdi.componentregistry.UserUnauthorizedException;
    41 import clarin.cmdi.componentregistry.components.CMDComponentSpec;
    42 import clarin.cmdi.componentregistry.model.AbstractDescription;
    43 import clarin.cmdi.componentregistry.model.Comment;
    44 import clarin.cmdi.componentregistry.model.ComponentDescription;
    45 import clarin.cmdi.componentregistry.model.ProfileDescription;
    46 import clarin.cmdi.componentregistry.model.CommentResponse;
    47 import clarin.cmdi.componentregistry.model.RegisterResponse;
    48 
    49 import com.sun.jersey.multipart.FormDataParam;
    50 import com.sun.jersey.spi.inject.Inject;
    5147
    5248@Path("/registry")
     
    134130    @Path("/components/{componentId}")
    135131    @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    136     public CMDComponentSpec getRegisteredComponent(@PathParam("componentId") String componentId,
     132    public Response getRegisteredComponent(@PathParam("componentId") String componentId,
    137133            @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) throws ComponentRegistryException {
    138134        LOG.info("Component with id: " + componentId + " is requested.");
    139         return getRegistry(userspace).getMDComponent(componentId);
     135        CMDComponentSpec mdComponent = getRegistry(userspace).getMDComponent(componentId);
     136        if (mdComponent == null) {
     137            return Response.status(Status.NOT_FOUND).build();
     138        } else {
     139            return Response.ok(mdComponent).build();
     140        }
    140141    }
    141142
     
    212213    @Path("/profiles/{profileId}")
    213214    @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    214     public CMDComponentSpec getRegisteredProfile(@PathParam("profileId") String profileId,
     215    public Response getRegisteredProfile(@PathParam("profileId") String profileId,
    215216            @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) throws ComponentRegistryException {
    216217        LOG.info("Profile with id: " + profileId + " is requested.");
    217         return getRegistry(userspace).getMDProfile(profileId);
     218        CMDComponentSpec mdProfile = getRegistry(userspace).getMDProfile(profileId);
     219        if (mdProfile == null) {
     220            return Response.status(Status.NOT_FOUND).build();
     221        } else {
     222            return Response.ok(mdProfile).build();
     223        }
    218224    }
    219225
  • ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/rest/ComponentRegistryRestServiceTest.java

    r1836 r1870  
    347347            profile = getResource().path("/registry/profiles/clarin.eu:cr1:profileXXXX").accept(MediaType.APPLICATION_XML).get(
    348348                    CMDComponentSpec.class);
    349             fail("Exception should have been thrown resource does not exist, HttpStatusCode 204");
     349            fail("Exception should have been thrown resource does not exist, HttpStatusCode 404");
    350350        } catch (UniformInterfaceException e) {
    351             assertEquals(204, e.getResponse().getStatus());
     351            assertEquals(404, e.getResponse().getStatus());
    352352        }
    353353    }
     
    558558        cResponse = getResource().path("/registry/profiles/" + profileDesc.getId()).accept(MediaType.APPLICATION_XML).get(
    559559                ClientResponse.class);
    560         // Should return 204 = no content
    561         assertEquals(204, cResponse.getStatus());
     560        // Should return 404 = not found
     561        assertEquals(404, cResponse.getStatus());
    562562        CMDComponentSpec spec = getAuthenticatedResource(
    563563                getResource().path("/registry/profiles/" + profileDesc.getId()).queryParam(USERSPACE_PARAM, "true")).accept(
     
    698698        cResponse = getResource().path("/registry/components/" + desc.getId()).accept(MediaType.APPLICATION_XML).get(
    699699                ClientResponse.class);
    700         // Should return 204 = no content
    701         assertEquals(204, cResponse.getStatus());
     700        // Should return 404 = not found
     701        assertEquals(404, cResponse.getStatus());
    702702        CMDComponentSpec spec = getUserComponent(desc);
    703703        assertNotNull(spec);
     
    966966    /**
    967967     * Two elements on the same level with the same name violates schematron rule, and should fail validation
    968      * @throws Exception
     968     *
     969     * @throws Exception
    969970     */
    970971    @Test
Note: See TracChangeset for help on using the changeset viewer.