Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2010 MediaShelf <http://www.yourmediashelf.com/> This file is part of fedora-client. fedora-client is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. fedora-client is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with fedora-client. If not, see <http://www.gnu.org/licenses/>.
 
 
 package com.yourmediashelf.fedora.client.response;
 
 
 
 
Base class for FedoraResponse implementations.

Author(s):
Edwin Shin
 
 public class FedoraResponseImpl implements FedoraResponse {
 
     private final ClientResponse cr;
 
     private final int status;
 
     private final NamespaceContextImpl nsCtx;
 
     private Boolean validateSchema = null;

    
Constructor for a FedoraResponseImpl.

Parameters:
cr
Throws:
com.yourmediashelf.fedora.client.FedoraClientException if the HTTP status code of the response is >= 400.
 
     public FedoraResponseImpl(ClientResponse cr)
             throws FedoraClientException {
         this. = cr;
          = new NamespaceContextImpl();
         .addNamespace("f""info:fedora/fedora-system:def/foxml#");
 
         this. = cr.getStatus();
         if ( >= 400) {
             String msg = cr.getEntity(String.class);
             throw new FedoraClientException(, String.format(
                     "HTTP %d Error: %s"msg));
         }
     }
 
     @Override
     public int getStatus() {
         return ;
     }
 
     @Override
     public InputStream getEntityInputStream() {
         return .getEntityInputStream();
     }
 
     @Override
     public <T> T getEntity(Class<T> c) {
         return .getEntity(c);
     }
 
     @Override
     public String getType() {
         return .getType().toString();
     }
 
     protected XPath getXPath() {
         XPathFactory factory = XPathFactory.newInstance();
         XPath xpath = factory.newXPath();
        xpath.setNamespaceContext();
        return xpath;
    }

    
Unmarshall the Fedora ClientResponse using the JAXB schema-generated classes (see: target/generated-sources/).

Parameters:
contextPath JAXB contextPath
Returns:
the unmarshalled XML
Throws:
com.yourmediashelf.fedora.client.FedoraClientException
    public Object unmarshallResponse(ContextPath contextPath)
            throws FedoraClientException {
        Object response = null;
        Schema schema = null;
        if (validateSchema()) {
            try {
                SchemaFactory schemaFactory =
                        SchemaFactory
                                .newInstance(.);
                schema = schemaFactory.newSchema();
            } catch (SAXException e) {
                throw new FedoraClientException(e.getMessage(), e);
            }
        }
        try {
            JAXBContext context = JAXBContext.newInstance(contextPath.path());
            Unmarshaller unmarshaller = context.createUnmarshaller();
            unmarshaller.setSchema(schema);
            response =
                    unmarshaller.unmarshal(new BufferedReader(
                            new InputStreamReader(getEntityInputStream())));
        } catch (JAXBException e) {
            throw new FedoraClientException(e.getMessage(), e);
        }
        return response;
    }
    private boolean validateSchema() {
        if ( == null) {
            String validate = System.getProperty("test.validate");
             = Boolean.parseBoolean(validate);
        }
        return .booleanValue();
    }

    
Enum for JAXB ContextPaths used by FedoraResponse implementations.

Author(s):
Edwin Shin
    public enum ContextPath {
        Access("com.yourmediashelf.fedora.generated.access"), Management(
                "com.yourmediashelf.fedora.generated.management");
        private final String contextPath;

        
Returns the associated contextPath.

Returns:
the JAXB ContextPath, e.g. "com.yourmediashelf.fedora.generated.access".
        public String path() {
            return ;
        }
        ContextPath(String contextPath) {
            this. = contextPath;
        }
    }
    @Override
    public void close() {
        .close();
    }
New to GrepCode? Check out our FAQ X