Changeset 1860


Ignore:
Timestamp:
04/05/12 14:13:08 (12 years ago)
Author:
twagoo
Message:

ComponentRegistry REST service:

  • Using CMDValidate 1.1 (see r1859)
  • Moved anonymous LSResourceResolver implementation into its own class
  • Passing this class to CMDValidate validator
Location:
ComponentRegistry/trunk/ComponentRegistry
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • ComponentRegistry/trunk/ComponentRegistry/pom.xml

    r1816 r1860  
    185185            <groupId>clarin.cmdi</groupId>
    186186            <artifactId>cmd-validate</artifactId>
    187             <version>1.0</version>
     187            <version>1.1</version>
    188188        </dependency>
    189189    </dependencies>
  • ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/MDMarshaller.java

    r1816 r1860  
    4141    private final static Logger LOG = LoggerFactory.getLogger(MDMarshaller.class);
    4242    /**
    43      * I define W3C_XML_SCHEMA_NS_URI here cannot get it from @see XMLConstants there is a conflict between stax-api and java5.
     43     * I define W3C_XML_SCHEMA_NS_URI here cannot get it from
     44     *
     45     * @see XMLConstants there is a conflict between stax-api and java5.
    4446     */
    4547    private static final String W3C_XML_SCHEMA_NS_URI = "http://www.w3.org/2001/XMLSchema";
     
    6567
    6668    /**
    67      * 
     69     *
    6870     * @param docClass
    6971     * @param inputStream
     
    102104    }
    103105
    104     public static Schema getCMDComponentSchema() {
     106    public static synchronized Schema getCMDComponentSchema() {
    105107        if (generalComponentSchema == null) {
    106108            try {
    107109                SchemaFactory schemaFactory = SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI);
    108                 schemaFactory.setResourceResolver(new LSResourceResolver() {
    109                     private CatalogResolver catRes = new CatalogResolver();
    110 
    111                     @Override
    112                     public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) {
    113                         InputSource resolveEntity = catRes.resolveEntity(publicId, systemId);
    114                         resolveEntity.setEncoding("UTF-8");
    115                         DOMImplementationLS domImplementation;
    116                         try {
    117                             domImplementation = (DOMImplementationLS) DOMImplementationRegistry.newInstance().getDOMImplementation("LS");
    118                         } catch (ClassCastException e) {
    119                             throw new RuntimeException(e);
    120                         } catch (ClassNotFoundException e) {
    121                             throw new RuntimeException(e);
    122                         } catch (InstantiationException e) {
    123                             throw new RuntimeException(e);
    124                         } catch (IllegalAccessException e) {
    125                             throw new RuntimeException(e);
    126                         }
    127                         LSInput lsInput = domImplementation.createLSInput();
    128                         lsInput.setEncoding("UTF-8");
    129                         lsInput.setByteStream(resolveEntity.getByteStream());
    130                         lsInput.setCharacterStream(resolveEntity.getCharacterStream());
    131                         return lsInput;
    132                     }
    133                 });
     110                schemaFactory.setResourceResolver(new ComponentRegistryResourceResolver());
    134111                generalComponentSchema = schemaFactory.newSchema(new URL(Configuration.getInstance().getGeneralComponentSchema()));
    135112            } catch (MalformedURLException e) {
     
    169146    }
    170147
    171    
    172    
    173148    private static String getSpecId(CMDComponentSpec spec) {
    174149        String result = "";
  • ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/ComponentRegistryRestService.java

    r1753 r1860  
    5252@Path("/registry")
    5353public class ComponentRegistryRestService {
    54 
     54   
    5555    @Context
    5656    private UriInfo uriInfo;
     
    6969    @Inject(value = "componentRegistryFactory")
    7070    private ComponentRegistryFactory componentRegistryFactory;
    71 
     71   
    7272    private ComponentRegistry getRegistry(boolean userspace) {
    7373        Principal userPrincipal = security.getUserPrincipal();
     
    7575        return getRegistry(userspace, userCredentials);
    7676    }
    77 
     77   
    7878    private ComponentRegistry getRegistry(boolean userspace, UserCredentials userCredentials) {
    7979        return componentRegistryFactory.getComponentRegistry(userspace, userCredentials);
     
    8181
    8282    /**
    83      * 
     83     *
    8484     * @return Principal of current request
    8585     * @throws IllegalArgumentException If no user principal found
     
    9292        return principal;
    9393    }
    94 
     94   
    9595    private UserCredentials getUserCredentials(Principal userPrincipal) {
    9696        UserCredentials userCredentials = null;
     
    100100        return userCredentials;
    101101    }
    102 
     102   
    103103    @GET
    104104    @Path("/components")
     
    111111        return components;
    112112    }
    113 
     113   
    114114    @GET
    115115    @Path("/profiles")
     
    118118            @QueryParam(METADATA_EDITOR_PARAM) @DefaultValue("false") boolean metadataEditor) throws ComponentRegistryException {
    119119        long start = System.currentTimeMillis();
    120 
     120       
    121121        List<ProfileDescription> profiles;
    122122        if (metadataEditor) {
     
    125125            profiles = getRegistry(userspace).getProfileDescriptions();
    126126        }
    127 
     127       
    128128        LOG.info("Releasing " + profiles.size() + " registered profiles into the world (" + (System.currentTimeMillis() - start)
    129129                + " millisecs)");
    130130        return profiles;
    131131    }
    132 
     132   
    133133    @GET
    134134    @Path("/components/{componentId}")
     
    139139        return getRegistry(userspace).getMDComponent(componentId);
    140140    }
    141 
     141   
    142142    @GET
    143143    @Path("/components/{componentId}/{rawType}")
     
    156156            if ("xml".equalsIgnoreCase(rawType)) {
    157157                result = new StreamingOutput() {
    158 
     158                   
    159159                    @Override
    160160                    public void write(OutputStream output) throws IOException, WebApplicationException {
     
    169169            } else if ("xsd".equalsIgnoreCase(rawType)) {
    170170                result = new StreamingOutput() {
    171 
     171                   
    172172                    @Override
    173173                    public void write(OutputStream output) throws IOException, WebApplicationException {
     
    178178                            throw new WebApplicationException(e, Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build());
    179179                        }
    180 
     180                       
    181181                    }
    182182                };
     
    191191        }
    192192    }
    193 
     193   
    194194    public ComponentRegistry findRegistry(String id, RegistryClosure<? extends AbstractDescription> clos) throws ComponentRegistryException {
    195195        AbstractDescription desc = null;
     
    208208        return result;
    209209    }
    210 
     210   
    211211    @GET
    212212    @Path("/profiles/{profileId}")
     
    217217        return getRegistry(userspace).getMDProfile(profileId);
    218218    }
    219 
     219   
    220220    @GET
    221221    @Path("/components/usage/{componentId}")
     
    227227            List<ComponentDescription> components = registry.getUsageInComponents(componentId);
    228228            List<ProfileDescription> profiles = registry.getUsageInProfiles(componentId);
    229 
     229           
    230230            LOG.info("Found " + components.size() + " components and " + profiles.size() + " profiles that use component " + componentId
    231231                    + " (" + (System.currentTimeMillis() - start) + " millisecs)");
    232 
     232           
    233233            List<AbstractDescription> usages = new ArrayList<AbstractDescription>(components.size() + profiles.size());
    234234            usages.addAll(components);
    235235            usages.addAll(profiles);
    236 
     236           
    237237            return usages;
    238238        } catch (ComponentRegistryException e) {
     
    241241        }
    242242    }
    243 
     243   
    244244    @GET
    245245    @Path("/profiles/{profileId}/comments")
     
    252252        return comments;
    253253    }
    254 
     254   
    255255    @GET
    256256    @Path("/components/{componentId}/comments")
     
    263263        return comments;
    264264    }
    265 
     265   
    266266    @GET
    267267    @Path("/profiles/{profileId}/comments/{commentId}")
     
    271271        return getRegistry(userspace).getSpecifiedCommentInProfile(profileId, commentId);
    272272    }
    273 
     273   
    274274    @GET
    275275    @Path("/components/{componentId}/comments/{commentId}")
     
    281281
    282282    /**
    283      * 
     283     *
    284284     * Purely helper method for my front-end (FLEX) which only does post/get requests. The query param is checked and the "proper" method is
    285285     * called.
     286     *
    286287     * @param profileId
    287288     * @param method
     
    298299        }
    299300    }
    300 
     301   
    301302    @POST
    302303    @Path("/profiles/{profileId}/comments/{commentId}")
     
    309310        }
    310311    }
    311 
     312   
    312313    @POST
    313314    @Path("/components/{componentId}/comments/{commentId}")
     
    320321        }
    321322    }
    322 
     323   
    323324    @POST
    324325    @Path("/profiles/{profileId}/publish")
     
    344345        }
    345346    }
    346 
     347   
    347348    @POST
    348349    @Path("/profiles/{profileId}/update")
     
    369370            return Response.status(Status.UNAUTHORIZED).entity(ex.getMessage()).build();
    370371        }
    371 
     372       
    372373    }
    373374
    374375    /**
    375      * 
     376     *
    376377     * Purely helper method for my front-end (FLEX) which van only do post/get requests. The query param is checked and the "proper" method
    377378     * is called.
     379     *
    378380     * @param componentId
    379381     * @param method
     
    390392        }
    391393    }
    392 
     394   
    393395    @POST
    394396    @Path("/components/{componentId}/publish")
     
    415417        }
    416418    }
    417 
     419   
    418420    @POST
    419421    @Path("/components/{componentId}/update")
     
    440442        }
    441443    }
    442 
     444   
    443445    private void updateDescription(AbstractDescription desc, String name, String description, String domainName, String group) {
    444446        desc.setName(name);
     
    448450        desc.setRegistrationDate(AbstractDescription.createNewDate());
    449451    }
    450 
     452   
    451453    @DELETE
    452454    @Path("/components/{componentId}")
     
    474476        return Response.ok().build();
    475477    }
    476 
     478   
    477479    @DELETE
    478480    @Path("/profiles/{profileId}")
     
    499501        return Response.ok().build();
    500502    }
    501 
     503   
    502504    @DELETE
    503505    @Path("/profiles/{profileId}/comments/{commentId}")
     
    530532        return Response.ok().build();
    531533    }
    532 
     534   
    533535    @DELETE
    534536    @Path("/components/{componentId}/comments/{commentId}")
     
    561563        return Response.ok().build();
    562564    }
    563 
     565   
    564566    @GET
    565567    @Path("/profiles/{profileId}/{rawType}")
     
    576578            checkAndThrowDescription(desc, profileId);
    577579            String fileName = desc.getName() + "." + rawType;
    578 
     580           
    579581            if ("xml".equalsIgnoreCase(rawType)) {
    580582                result = new StreamingOutput() {
    581 
     583                   
    582584                    @Override
    583585                    public void write(OutputStream output) throws IOException, WebApplicationException {
     
    592594            } else if ("xsd".equalsIgnoreCase(rawType)) {
    593595                result = new StreamingOutput() {
    594 
     596                   
    595597                    @Override
    596598                    public void write(OutputStream output) throws IOException, WebApplicationException {
     
    613615        }
    614616    }
    615 
     617   
    616618    private void checkAndThrowDescription(AbstractDescription desc, String id) {
    617619        if (desc == null) {
     
    619621        }
    620622    }
    621 
     623   
    622624    private Response createDownloadResponse(StreamingOutput result, String fileName) {
    623625        //Making response so it triggers browsers native save as dialog.
     
    625627                "attachment; filename=\"" + fileName + "\"").entity(result).build();
    626628        return response;
    627 
    628     }
    629 
     629       
     630    }
     631   
    630632    @POST
    631633    @Path("/profiles")
     
    651653        }
    652654    }
    653 
     655   
    654656    @POST
    655657    @Path("/components")
     
    675677        }
    676678    }
    677 
     679   
    678680    @POST
    679681    @Path("/components/{componentId}/comments")
     
    701703        }
    702704    }
    703 
     705   
    704706    @POST
    705707    @Path("/profiles/{profileId}/comments")
     
    727729        }
    728730    }
    729 
     731   
    730732    @GET
    731733    @Path("/pingSession")
     
    742744        return Response.ok().entity("<session stillActive=\"" + stillActive + "\"/>").build();
    743745    }
    744 
     746   
    745747    private Response register(InputStream input, AbstractDescription desc, UserCredentials userCredentials, boolean userspace,
    746748            RegisterAction action) {
     
    777779            }
    778780            response.setIsProfile(desc.isProfile());
     781            LOG.info("Registration of {} successful: {}", response.getDescription().getId(), response.isRegistered());
    779782            return Response.ok(response).build();
    780783        } finally {
     
    786789        }
    787790    }
    788 
     791   
    789792    private Response registerComment(InputStream input, ComponentRegistry registry, boolean userspace,
    790793            AbstractDescription description, Principal principal, UserCredentials userCredentials) {
     
    806809                    }
    807810                }
    808 
     811               
    809812                int returnCode = registry.registerComment(com, principal.getName());
    810813                if (returnCode == 0) {
     
    832835        }
    833836    }
    834 
     837   
    835838    private ComponentDescription createNewComponentDescription() {
    836839        ComponentDescription desc = ComponentDescription.createNewDescription();
     
    838841        return desc;
    839842    }
    840 
     843   
    841844    private ProfileDescription createNewProfileDescription() {
    842845        ProfileDescription desc = ProfileDescription.createNewDescription();
     
    844847        return desc;
    845848    }
    846 
     849   
    847850    private Comment createNewComment() {
    848851        Comment com = Comment.createANewComment();
    849852        return com;
    850853    }
    851 
     854   
    852855    private String createXlink(String id) {
    853856        URI uri = uriInfo.getRequestUriBuilder().path(id).build();
    854857        return uri.toString();
    855858    }
    856 
     859   
    857860    private void validate(RegisterResponse response, Validator... validators) {
    858861        for (Validator validator : validators) {
     
    864867        }
    865868    }
    866 
     869   
    867870    private void validateComment(CommentResponse response, Validator... validators) {
    868871        for (Validator validator : validators) {
  • ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/MDValidator.java

    r1816 r1860  
    33import clarin.cmdi.componentregistry.ComponentRegistry;
    44import clarin.cmdi.componentregistry.ComponentRegistryException;
     5import clarin.cmdi.componentregistry.ComponentRegistryResourceResolver;
    56import clarin.cmdi.componentregistry.Configuration;
    67import clarin.cmdi.componentregistry.MDMarshaller;
     
    2829
    2930public class MDValidator implements Validator {
    30 
     31   
    3132    private final static Logger LOG = LoggerFactory.getLogger(MDValidator.class);
    3233    static final String MISMATCH_ERROR = "Cannot register component as a profile or vica versa.";
     
    4950
    5051    /**
    51      *
    52      * @param input In order to validate the input is consumed. So use @see getCMDComponentSpec to get the parsed CMDComponentSpec.
     52     *
     53     * @param input In order to validate the input is consumed. So use
     54     * @see getCMDComponentSpec to get the parsed CMDComponentSpec.
    5355     * @param desc
    54      * @param registry (registry you currently used)
    55      * @param userRegistry can be null, We get user registry as well so we can give nice error messages if needed. Can be the same as @param registry
     56     * @param registry (registry you currently used)
     57     * @param userRegistry can be null, We get user registry as well so we can give nice error messages if needed. Can be the same as
     58     * @param registry
    5659     */
    5760    public MDValidator(InputStream input, AbstractDescription description, ComponentRegistry registry, ComponentRegistry userRegistry, ComponentRegistry publicRegistry) {
     
    6265        this.publicRegistry = publicRegistry;
    6366    }
    64 
     67   
    6568    @Override
    6669    public List<String> getErrorMessages() {
    6770        return errorMessages;
    6871    }
    69 
     72   
    7073    @Override
    7174    public boolean validate() {
    7275        try {
    7376            clarin.cmdi.schema.cmd.Validator validator = new clarin.cmdi.schema.cmd.Validator(new URL(Configuration.getInstance().getGeneralComponentSchema()));
     77            validator.setResourceResolver(new ComponentRegistryResourceResolver());
    7478            // We may need to reuse the input stream, so save it to a byte array first
    7579            byte[] inputBytes = getBytesFromInputStream();
     
    109113        return errorMessages.isEmpty();
    110114    }
    111 
     115   
    112116    private byte[] getBytesFromInputStream() throws IOException {
    113117        int len;
    114118        byte[] b = new byte[4096];
    115119        final ByteArrayOutputStream bOS = new ByteArrayOutputStream();
    116 
     120       
    117121        while ((len = input.read(b)) > 0) {
    118122            bOS.write(b, 0, len);
    119123        }
    120 
     124       
    121125        return bOS.toByteArray();
    122126    }
    123 
     127   
    124128    private void validateComponents(List<CMDComponentType> cmdComponents) throws ComponentRegistryException {
    125129        for (CMDComponentType cmdComponentType : cmdComponents) {
     
    128132        }
    129133    }
    130 
     134   
    131135    private void validateDescribedComponents(CMDComponentType cmdComponentType) throws ComponentRegistryException {
    132136        checkPublicComponents(cmdComponentType);
    133137    }
    134 
     138   
    135139    private void checkPublicComponents(CMDComponentType cmdComponentType) throws ComponentRegistryException {
    136140        if (isDefinedInSeparateFile(cmdComponentType)) {
     
    157161                    }
    158162                }
    159 
     163               
    160164            }
    161165        }
    162166    }
    163 
     167   
    164168    private boolean isDefinedInSeparateFile(CMDComponentType cmdComponentType) {
    165169        return cmdComponentType.getName() == null;
    166170    }
    167 
     171   
    168172    public CMDComponentSpec getCMDComponentSpec() {
    169173        return spec;
Note: See TracChangeset for help on using the changeset viewer.