Ignore:
Timestamp:
08/15/14 16:02:36 (10 years ago)
Author:
olhsha@mpi.nl
Message:

fixing serialisation issues for List<String>. Adding unit tests for goup service

File:
1 edited

Legend:

Unmodified
Added
Removed
  • ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/ComponentRegistryRestService.java

    r5552 r5553  
    2828import clarin.cmdi.componentregistry.rss.RssCreatorDescriptions;
    2929import com.google.common.collect.Lists;
    30 import com.sun.jersey.api.client.ClientResponse;
    31 import com.sun.jersey.api.client.UniformInterfaceException;
    3230
    3331import com.sun.jersey.api.core.InjectParam;
     
    6159import javax.ws.rs.WebApplicationException;
    6260import javax.ws.rs.core.Context;
     61import javax.ws.rs.core.GenericEntity;
    6362import javax.ws.rs.core.MediaType;
    6463import javax.ws.rs.core.Response;
     
    6867import javax.ws.rs.core.UriInfo;
    6968import javax.xml.bind.JAXBException;
     69import javax.xml.bind.annotation.XmlElement;
     70import javax.xml.bind.annotation.XmlRootElement;
    7071
    7172import org.slf4j.Logger;
     
    189190                    "Releasing {} registered components into the world ({} millisecs)",
    190191                    result.size(), (System.currentTimeMillis() - start));
    191            
     192
    192193            return result;
    193194        } catch (AuthenticationFailException e) {
     
    226227                    "Releasing {} registered components into the world ({} millisecs)",
    227228                    result.size(), (System.currentTimeMillis() - start));
    228              
     229
    229230            return result;
    230231        } catch (AuthenticationFailException e) {
     
    553554    }
    554555
    555    
    556556    @Override
    557557    @POST
     
    568568    }
    569569
    570    
    571570    @Override
    572571    @POST
     
    881880
    882881        LOG.info("Component with id: {} deleted.", componentId);
    883         return Response.ok("Component with id" + componentId+" deleted.").build();
     882        return Response.ok("Component with id" + componentId + " deleted.").build();
    884883    }
    885884
     
    17921791        }
    17931792    }
     1793
     1794    // Group Service (added by Olha)
     1795    @Override
     1796    @POST
     1797    @Path("/groups/create")
     1798    @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML,
     1799        MediaType.APPLICATION_JSON})
     1800    public Response createNewGroup(@QueryParam("groupName") String groupName) throws IOException {
     1801
     1802        try {
     1803            Principal principal = this.checkAndGetUserPrincipal();
     1804            Number id = groupService.createNewGroup(groupName, principal.getName());
     1805            return Response.ok("Group with the name " + groupName + " is created and given an id " + id).build();
     1806        } catch (AuthenticationFailException e) {
     1807            return Response.status(Status.UNAUTHORIZED).build();
     1808        }
     1809    }
     1810
     1811    @Override
     1812    @GET
     1813    @Path("/groups/principal")
     1814    @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML,
     1815        MediaType.APPLICATION_JSON})
     1816    public List<Group> getGroupsOwnedByUser(@QueryParam("principalName") String pricipalName) throws IOException {
     1817
     1818        try {
     1819            Principal principal = this.checkAndGetUserPrincipal();
     1820            return groupService.getGroupsOwnedByUser(principal.getName());
     1821        } catch (AuthenticationFailException e) {
     1822            response.sendError(Status.UNAUTHORIZED.getStatusCode());
     1823            return new ArrayList<Group>();
     1824        }
     1825    }
     1826
     1827    @Override
     1828    @GET
     1829    @Path("/groups/names")
     1830    public Response listGroupNames() throws IOException {
     1831
     1832        try {
     1833            Principal principal = this.checkAndGetUserPrincipal();
     1834            List<String> result = groupService.listGroupNames();
     1835            //final GenericEntity<List<String>> entity  = new GenericEntity<List<String>>(result){};
     1836            //return Response.status(Status.OK).entity(entity).build();
     1837            StringsWrapper ids = new StringsWrapper();
     1838            ids.setStrings(result);
     1839            return Response.status(Status.OK).entity(ids).build();
     1840        } catch (AuthenticationFailException e) {
     1841            return Response.status(Status.UNAUTHORIZED).build();
     1842        }
     1843    }
     1844
     1845    @Override
     1846    @GET
     1847    @Path("/groups/ownership")
     1848    public Response isOwner(@QueryParam("groupName") String groupName) throws IOException {
     1849
     1850        try {
     1851            Principal principal = this.checkAndGetUserPrincipal();
     1852            Boolean isOwner = groupService.isUserOwnerOfGroup(groupName, principal.getName());
     1853            return Response.ok(isOwner.toString()).build();
     1854        } catch (AuthenticationFailException e) {
     1855            return Response.serverError().status(Status.UNAUTHORIZED).build();
     1856        }
     1857    }
     1858
     1859    @Override
     1860    @POST
     1861    @Path("/groups/makemember")
     1862    @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML,
     1863        MediaType.APPLICATION_JSON})
     1864    public Response makeGroupMember(@QueryParam("groupName") String groupName, @QueryParam("principalName") String principalName) throws IOException {
     1865        try {
     1866            ComponentRegistry registry = this.getBaseRegistry();
     1867            Number id = registry.makeGroupMember(principalName, groupName);
     1868            return Response.ok(id.toString()).build();
     1869        } catch (UserUnauthorizedException e) {
     1870            return Response.status(Status.FORBIDDEN).entity(e.getMessage()).build();
     1871        } catch (ItemNotFoundException e) {
     1872            return Response.status(Status.NOT_FOUND).entity(e.getMessage()).build();
     1873       
     1874        } catch (AuthenticationFailException e) {
     1875            return Response.status(Status.UNAUTHORIZED).build();
     1876        }
     1877    }
     1878   
     1879//    @Override
     1880//    @DELETE
     1881//    @Path("/groups/removemember")
     1882//    @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML,
     1883//        MediaType.APPLICATION_JSON})
     1884//    public Response removeGroupMember(@QueryParam("groupName") String groupName, @QueryParam("principalName") String principalName) throws IOException {
     1885//        try {
     1886//            ComponentRegistry registry = this.getBaseRegistry();
     1887//            Number id = registry.removeGroupMember(principalName, groupName);
     1888//            return Response.ok(id.toString()).build();
     1889//        } catch (UserUnauthorizedException e) {
     1890//            return Response.status(Status.FORBIDDEN).entity(e.getMessage()).build();
     1891//        } catch (ItemNotFoundException e) {
     1892//            return Response.status(Status.NOT_FOUND).entity(e.getMessage()).build();
     1893//       
     1894//        } catch (AuthenticationFailException e) {
     1895//            return Response.status(Status.UNAUTHORIZED).build();
     1896//        }
     1897//    }
     1898
     1899    @Override
     1900    @GET
     1901    @Path("/groups/profiles")
     1902    public Response listProfiles(@QueryParam("groupId") String groupId) throws IOException {
     1903
     1904        try {
     1905            Principal principal = this.checkAndGetUserPrincipal();
     1906            List<String> result = groupService.getProfileIdsInGroup(Long.parseLong(groupId));
     1907            StringsWrapper ids = new StringsWrapper();
     1908            ids.setStrings(result);
     1909            return Response.status(Status.OK).entity(ids).build();
     1910        } catch (AuthenticationFailException e) {
     1911            return Response.status(Status.UNAUTHORIZED).build();
     1912        }
     1913    }
     1914
     1915    @Override
     1916    @GET
     1917    @Path("/groups/components")
     1918    public Response listComponents(@QueryParam("groupId") String groupId) throws IOException {
     1919
     1920        try {
     1921            Principal principal = this.checkAndGetUserPrincipal();
     1922            List<String> result = groupService.getComponentIdsInGroup(Long.parseLong(groupId));
     1923            StringsWrapper ids = new StringsWrapper();
     1924            ids.setStrings(result);
     1925            return Response.status(Status.OK).entity(ids).build();
     1926        } catch (AuthenticationFailException e) {
     1927            response.sendError(Status.UNAUTHORIZED.getStatusCode());
     1928            return Response.status(Status.UNAUTHORIZED).build();
     1929        }
     1930    }
     1931
     1932    @Override
     1933    @GET
     1934    @Path("/groups/nameById")
     1935    public Response getGroupNameById(@QueryParam("groupId") String groupId) throws IOException {
     1936
     1937        try {
     1938            Principal principal = this.checkAndGetUserPrincipal();
     1939            String name = groupService.getGroupNameById(Long.parseLong(groupId));
     1940            return Response.ok(name).build();
     1941        } catch (AuthenticationFailException e) {
     1942            return Response.status(Status.UNAUTHORIZED).build();
     1943        } catch (ItemNotFoundException e) {
     1944            return Response.status(Status.NOT_FOUND).build();
     1945        }
     1946    }
     1947
     1948    @Override
     1949    @GET
     1950    @Path("/groups/idByName")
     1951    public Response getGroupIdByName(@QueryParam("groupName") String groupName) throws IOException {
     1952
     1953        try {
     1954            Principal principal = this.checkAndGetUserPrincipal();
     1955            Number id = groupService.getGroupIdByName(groupName);
     1956            return Response.ok(id.toString()).build();
     1957        } catch (AuthenticationFailException e) {
     1958            return Response.status(Status.UNAUTHORIZED).build();
     1959        } catch (ItemNotFoundException e) {
     1960            return Response.status(Status.NOT_FOUND).build();
     1961        }
     1962    }
     1963
     1964    @XmlRootElement(name = "Identifiers")
     1965    public static class StringsWrapper {
     1966
     1967        @XmlElement(name = "item")
     1968        List<String> strings = new ArrayList<String>();
     1969
     1970        StringsWrapper() {
     1971        }
     1972
     1973        public void setStrings(List<String> strings) {
     1974            this.strings = strings;
     1975        }
     1976       
     1977       
     1978    }
    17941979}
Note: See TracChangeset for help on using the changeset viewer.