FileDocCategorySizeDatePackage
GDataService.javaAPI DocApache Lucene 2.1.017619Wed Feb 14 10:46:06 GMT 2007org.apache.lucene.gdata.server

GDataService

public class GDataService extends Object implements Service
default implementation of the {@link org.apache.lucene.gdata.server.Service} interface.
author
Simon Willnauer

Fields Summary
private static final Log
LOG
protected org.apache.lucene.gdata.storage.Storage
storage
protected org.apache.lucene.gdata.server.registry.GDataServerRegistry
registry
private static final com.google.gdata.data.Generator
generator
private static final String
generatorName
private static final String
generatorURI
private static final String
XMLMIME
protected final org.apache.lucene.gdata.server.registry.EntryEventMediator
entryEventMediator
Constructors Summary
protected GDataService()

     
        generator = new Generator();
        generator.setName(generatorName);
        generator.setUri(generatorURI);
        generator.setVersion("0.1");
    
        try {
            StorageController controller = GDataServerRegistry.getRegistry()
                    .lookup(StorageController.class,
                            ComponentType.STORAGECONTROLLER);
            if (controller == null)
                throw new StorageException(
                        "StorageController is not registered");
            this.storage = controller.getStorage();
            this.entryEventMediator = GDataServerRegistry.getRegistry().getEntryEventMediator();
        } catch (StorageException e) {
            LOG
                    .fatal(
                            "Can't get Storage Instance -- can't serve any requests",
                            e);
            ServiceException ex = new ServiceException(
                    "Can't get Storage instance" + e.getMessage(), e,GDataResponse.SERVER_ERROR);
            ex.setStackTrace(e.getStackTrace());
            throw ex;
        }
    
Methods Summary
private voidbuildDynamicFeedElements(GDataRequest request, com.google.gdata.data.BaseFeed feed)

        feed.setGenerator(generator);
        feed.setItemsPerPage(request.getItemsPerPage());
        feed.setStartIndex(request.getStartIndex());
        feed.setId(request.getContextPath());
        List<Link> links = feed.getLinks();
        links.add(
                buildLink(Link.Rel.SELF, Link.Type.ATOM, request.getSelfId()));
        links.add(
                buildLink(Link.Rel.NEXT, XMLMIME, request.getNextId()));
        String prevLink = request.getPreviousId();
        if(prevLink != null)
            links.add(buildLink(Link.Rel.PREVIOUS,XMLMIME,prevLink));
        

    
private org.apache.lucene.gdata.data.ServerBaseEntrybuildEntry(GDataRequest request, GDataResponse response)

        try {
            ServerBaseEntry entry = new ServerBaseEntry(GDataEntityBuilder
                    .buildEntry(request));
            return entry;

        } catch (ParseException e) {
            ServiceException ex = new ServiceException(
                    "Could not parse entry from incoming request", e, GDataResponse.BAD_REQUEST);
            ex.setStackTrace(e.getStackTrace());
            throw ex;
        } catch (IOException e) {
            ServiceException ex = new ServiceException(
                    "Could not read or open input stream", e, GDataResponse.BAD_REQUEST);
            ex.setStackTrace(e.getStackTrace());
            throw ex;
        }
    
private com.google.gdata.data.LinkbuildLink(java.lang.String rel, java.lang.String type, java.lang.String href)

        Link retVal = new Link();
        retVal.setHref(href);
        retVal.setRel(rel);
        retVal.setType(type);
        return retVal;
    
public voidclose()

see
org.apache.lucene.gdata.server.Service#close()

        this.storage.close();
    
public com.google.gdata.data.BaseEntrycreateEntry(GDataRequest request, GDataResponse response)

see
org.apache.lucene.gdata.server.Service#createEntry(org.apache.lucene.gdata.server.GDataRequest, org.apache.lucene.gdata.server.GDataResponse)


        if (LOG.isInfoEnabled())
            LOG.info("create Entry for feedId: " + request.getFeedId());

        ServerBaseEntry entry = buildEntry(request, response);
        entry.setFeedId(request.getFeedId());
        entry.setServiceConfig(request.getConfigurator());
        BaseEntry tempEntry = entry.getEntry();
        tempEntry.setPublished(getCurrentDateTime());
        tempEntry.setUpdated(getCurrentDateTime());
        BaseEntry retVal = null;
        removeDynamicElements(entry.getEntry());
        try {
            retVal = this.storage.storeEntry(entry);
        } catch (Exception e) {
            
            ServiceException ex = new ServiceException("Could not store entry",
                    e,GDataResponse.SERVER_ERROR);
            ex.setStackTrace(e.getStackTrace());
            throw ex;
        }
        this.entryEventMediator.entryAdded(entry);
        return retVal;
    
public com.google.gdata.data.BaseEntrydeleteEntry(GDataRequest request, GDataResponse response)

see
org.apache.lucene.gdata.server.Service#deleteEntry(org.apache.lucene.gdata.server.GDataRequest, org.apache.lucene.gdata.server.GDataResponse)


        ServerBaseEntry entry = new ServerBaseEntry();
        entry.setServiceConfig(request.getConfigurator());
        entry.setFeedId(request.getFeedId());
        entry.setId(request.getEntryId());
        setVersionId(entry,request,response);
        if (entry.getId() == null)
            throw new ServiceException(
                    "entry id is null -- can not delete null entry",GDataResponse.SERVER_ERROR);
        try {
            this.storage.deleteEntry(entry);
            
        } catch (ResourceNotFoundException e) {
            
            ServiceException ex = new ServiceException(
                    "Could not delete entry", e,GDataResponse.BAD_REQUEST);
            ex.setStackTrace(e.getStackTrace());
            throw ex;
        }catch (ModificationConflictException e) {
            
            ServiceException ex = new ServiceException(
                    "Could not delete entry - version conflict",e, GDataResponse.CONFLICT);
            ex.setStackTrace(e.getStackTrace());
            throw ex;  
        }catch (StorageException e) {
            
            ServiceException ex = new ServiceException(
                    "Could not delete entry", e,GDataResponse.SERVER_ERROR);
            ex.setStackTrace(e.getStackTrace());
            throw ex;
        }
        this.entryEventMediator.entryDeleted(entry);
        //TODO change ret value
        return null;
    
private voiddynamicElementEntryStragey(com.google.gdata.data.BaseEntry entry, GDataRequest request)

        setSelfLink(entry, request.getContextPath());
    
protected voiddynamicElementFeedStragey(com.google.gdata.data.BaseFeed feed, GDataRequest request)

         buildDynamicFeedElements(request, feed);
        List<BaseEntry> entryList = feed.getEntries();
        for (BaseEntry entry : entryList) {
            String id = new StringBuilder(request.getContextPath()).append(entry.getId()).append("/").toString();
            setSelfLink(entry, id);
        }

    
protected com.google.gdata.data.DateTimegetCurrentDateTime()

        return new DateTime(System.currentTimeMillis(),0);
    
public java.util.DategetEntryLastModified(java.lang.String entryId, java.lang.String feedId)

see
org.apache.lucene.gdata.server.Service#getEntryLastModified(java.lang.String, java.lang.String)

            try {
                return new Date(this.storage.getEntryLastModified(entryId, feedId));
            } catch (StorageException e) {
                ServiceException ex = new ServiceException(
                        "Could not get Last update for entry  -- "+entryId, e, GDataResponse.SERVER_ERROR);
                ex.setStackTrace(e.getStackTrace());
                throw ex;
            }
        
    
public com.google.gdata.data.BaseFeedgetFeed(GDataRequest request, GDataResponse response)

see
org.apache.lucene.gdata.server.Service#getFeed(org.apache.lucene.gdata.server.GDataRequest, org.apache.lucene.gdata.server.GDataResponse)


        ServerBaseFeed feed = new ServerBaseFeed();
        feed.setId(request.getFeedId());
        feed.setStartIndex(request.getStartIndex());
        feed.setItemsPerPage(request.getItemsPerPage());
        feed.setServiceConfig(request.getConfigurator());
        try {
            /*
             * the strategy executes either a search or a direct access.
             */
            BaseFeed retVal = this.storage.getFeed(feed);
            dynamicElementFeedStragey(retVal, request);

            return retVal;
            /*
             * Resource not found will be detected in Gdata request.
             * the request queries the storage for the feed to get the service for the feed
             */
        } catch (StorageException e) {
            ServiceException ex = new ServiceException("Could not get feed", e,GDataResponse.NOT_FOUND);
            ex.setStackTrace(e.getStackTrace());
            throw ex;
        }

    
public java.util.DategetFeedLastModified(java.lang.String feedId)

see
org.apache.lucene.gdata.server.Service#getFeedLastModified(java.lang.String)

        try {
            return new Date(this.storage.getFeedLastModified(feedId));
           
        } catch (StorageException e) {
            ServiceException ex = new ServiceException(
                    "Could not get Last update for feed -- "+feedId, e, GDataResponse.SERVER_ERROR);
            ex.setStackTrace(e.getStackTrace());
            throw ex;
        }

    
public com.google.gdata.data.BaseEntrygetSingleEntry(GDataRequest request, GDataResponse response)

see
org.apache.lucene.gdata.server.Service#getSingleEntry(org.apache.lucene.gdata.server.GDataRequest, org.apache.lucene.gdata.server.GDataResponse)


        try {
            ServerBaseEntry entry = new ServerBaseEntry();
            entry.setServiceConfig(request.getConfigurator());
            entry.setFeedId(request.getFeedId());
            entry.setId(request.getEntryId());

            
            if(entry.getId() == null){
                throw new ServiceException("entry is null can't get entry", GDataResponse.BAD_REQUEST);
            }
                
            BaseEntry retVal = null;
            retVal = this.storage.getEntry(entry);
            dynamicElementEntryStragey(retVal, request);
            return retVal;
        } catch (ResourceNotFoundException e) {
            ServiceException ex = new ServiceException(
                    "Could not get entry", e, GDataResponse.BAD_REQUEST);
            ex.setStackTrace(e.getStackTrace());
            throw ex;
        } catch (StorageException e) {
            ServiceException ex = new ServiceException("Could not get entry", e, GDataResponse.SERVER_ERROR);
            ex.setStackTrace(e.getStackTrace());
            throw ex;
        }
    
private voidremoveDynamicElements(com.google.gdata.data.BaseEntry entry)

        Link selfLink = entry.getSelfLink();
        if(selfLink != null)
            entry.getLinks().remove(selfLink);
        Link editLink = entry.getEditLink();
        if(editLink != null)
            entry.getLinks().remove(editLink);
    
private com.google.gdata.data.BaseEntrysetSelfLink(com.google.gdata.data.BaseEntry entry, java.lang.String id)

        Link self = buildLink(Link.Rel.SELF, XMLMIME, id);
        StringBuilder builder = new StringBuilder(id);
        builder.append(entry.getVersionId());
        Link edit = buildLink(Link.Rel.ENTRY_EDIT,XMLMIME,builder.toString());
        List<Link> list = entry.getLinks();
        list.add(edit);
        list.add(self);
        return entry;
    
private org.apache.lucene.gdata.data.ServerBaseEntrysetVersionId(org.apache.lucene.gdata.data.ServerBaseEntry entry, GDataRequest request, GDataResponse response)

        try{
            entry.setVersion(Integer.parseInt(request.getEntryVersion()));
            return entry;
        }catch (Exception e) {
            LOG.error("Can not parse entry version -- version is not an integer -- versionid: "+request.getEntryVersion(),e);
            throw new ServiceException("Can not parse entry version -- version is not an integer -- versionid: "+request.getEntryVersion(),e, GDataResponse.BAD_REQUEST);
           
        }
    
public com.google.gdata.data.BaseEntryupdateEntry(GDataRequest request, GDataResponse response)

see
org.apache.lucene.gdata.server.Service#updateEntry(org.apache.lucene.gdata.server.GDataRequest, org.apache.lucene.gdata.server.GDataResponse)


        ServerBaseEntry entry = buildEntry(request, response);
        entry.setFeedId(request.getFeedId());
        setVersionId(entry,request,response);
        entry.setServiceConfig(request.getConfigurator());
        if (LOG.isInfoEnabled())
            LOG.info("update Entry" + entry.getId() + " for feedId: "
                    + request.getFeedId());
        if (entry.getId() == null) {
            throw new ServiceException("Entry id is null can not update entry",GDataResponse.BAD_REQUEST);
        }
        if (!entry.getId().equals(request.getEntryId())) {
            if (LOG.isInfoEnabled())
                LOG
                        .info("Entry id in the entry xml does not match the requested resource -- XML-ID:"
                                + entry.getId()
                                + "; Requested resource: "
                                + request.getEntryId());

            throw new ServiceException(
                    "Entry id in the entry xml does not match the requested resource",GDataResponse.BAD_REQUEST);
        }
        BaseEntry tempEntry = entry.getEntry();
        tempEntry.setUpdated(getCurrentDateTime());
        removeDynamicElements(entry.getEntry());
        
        BaseEntry retVal = null;
     
        try {
            retVal = this.storage.updateEntry(entry);
        } catch (ResourceNotFoundException e) {
            
            ServiceException ex = new ServiceException(
                    "Could not update entry", e,GDataResponse.BAD_REQUEST);
            ex.setStackTrace(e.getStackTrace());
            throw ex;
        }catch (ModificationConflictException e) {
            ServiceException ex = new ServiceException(
                    "Could not update entry - version conflict", e,GDataResponse.CONFLICT);
            ex.setStackTrace(e.getStackTrace());
            throw ex;
        }catch (StorageException e) {

            ServiceException ex = new ServiceException(
                    "Could not update entry", e,GDataResponse.SERVER_ERROR);
            ex.setStackTrace(e.getStackTrace());
            throw ex;
        }
        this.entryEventMediator.entryUpdated(entry);
        return retVal;