Package org.restlet.ext.atom

Examples of org.restlet.ext.atom.Entry


                initiateInlineMixedContent();
                this.currentLink.setContent(currentContent);
                startLink(this.currentLink);
            } else if (localName.equalsIgnoreCase("entry")) {
                if (this.state == State.FEED) {
                    this.currentEntry = new Entry();
                    this.state = State.FEED_ENTRY;
                }
                startEntry(this.currentEntry);
            } else if (localName.equals("category")) {
                this.currentCategory = new Category();
View Full Code Here


     *            The entity to put.
     * @throws Exception
     */
    public void addEntity(String entitySetName, Object entity) throws Exception {
        if (entity != null) {
            Entry entry = toEntry(entity);

            ClientResource resource = createResource(entitySetName);
            if (getMetadata() == null) {
                throw new Exception("Can't add entity to this entity set "
                        + resource.getReference()
                        + " due to the lack of the service's metadata.");
            }

            try {
                // TODO Fix chunked request with net client connector
                ByteArrayOutputStream o = new ByteArrayOutputStream();
                entry.write(o);
                StringRepresentation r = new StringRepresentation(o.toString(),
                        MediaType.APPLICATION_ATOM);
                Representation rep = resource.post(r);
                EntryContentHandler<?> entryContentHandler = new EntryContentHandler<Object>(
                        entity.getClass(), (Metadata) getMetadata(),
                        getLogger());
                Feed feed = new Feed();
                feed.getEntries().add(new Entry(rep, entryContentHandler));
            } catch (ResourceException re) {
                throw new ResourceException(re.getStatus(),
                        "Can't add entity to this entity set "
                                + resource.getReference());
            } finally {
View Full Code Here

     * @param entity
     *            The entity to wrap.
     * @return The Atom entry object that corresponds to the given entity.
     */
    public Entry toEntry(final Object entity) {
        Entry result = null;

        if (entity != null) {
            Metadata metadata = (Metadata) getMetadata();
            EntityType type = metadata.getEntityType(entity.getClass());
            if (type != null) {
                final SaxRepresentation r = new SaxRepresentation(
                        MediaType.APPLICATION_XML) {

                    @Override
                    public void write(XmlWriter writer) throws IOException {
                        try {
                            // Attribute for nullable values.
                            AttributesImpl nullAttrs = new AttributesImpl();
                            nullAttrs.addAttribute(
                                    WCF_DATASERVICES_METADATA_NAMESPACE,
                                    "null", null, "boolean", "true");

                            writer.forceNSDecl(
                                    WCF_DATASERVICES_METADATA_NAMESPACE, "m");
                            writer.forceNSDecl(WCF_DATASERVICES_NAMESPACE, "d");
                            writer.startElement(
                                    WCF_DATASERVICES_METADATA_NAMESPACE,
                                    "properties");
                            write(writer, entity, nullAttrs);
                            writer.endElement(
                                    WCF_DATASERVICES_METADATA_NAMESPACE,
                                    "properties");
                        } catch (SAXException e) {
                            throw new IOException(e.getMessage());
                        }
                    }

                    private void write(XmlWriter writer, Object entity,
                            AttributesImpl nullAttrs) throws SAXException {
                        for (Field field : entity.getClass()
                                .getDeclaredFields()) {
                            String getter = "get"
                                    + field.getName().substring(0, 1)
                                            .toUpperCase()
                                    + field.getName().substring(1);
                            Property prop = ((Metadata) getMetadata())
                                    .getProperty(entity, field.getName());
                            if (prop != null) {
                                writeProperty(writer, entity, prop, getter,
                                        nullAttrs);
                            }
                        }
                    }

                    private void writeProperty(XmlWriter writer, Object entity,
                            Property prop, String getter,
                            AttributesImpl nullAttrs) throws SAXException {
                        for (Method method : entity.getClass()
                                .getDeclaredMethods()) {
                            if (method.getReturnType() != null
                                    && getter.equals(method.getName())
                                    && method.getParameterTypes().length == 0) {
                                Object value = null;
                                try {
                                    value = method.invoke(entity,
                                            (Object[]) null);
                                } catch (Exception e) {

                                }
                                if (value != null) {
                                    writer.startElement(
                                            WCF_DATASERVICES_NAMESPACE,
                                            prop.getName());
                                    if (prop instanceof ComplexProperty) {
                                        write(writer, value, nullAttrs);
                                    } else {
                                        writer.characters(TypeUtils.toEdm(
                                                value, prop.getType()));
                                    }
                                    writer.endElement(
                                            WCF_DATASERVICES_NAMESPACE,
                                            prop.getName());
                                } else {
                                    if (prop.isNullable()) {
                                        writer.emptyElement(
                                                WCF_DATASERVICES_NAMESPACE,
                                                prop.getName(), prop.getName(),
                                                nullAttrs);
                                    } else {
                                        getLogger().warning(
                                                "The following property has a null value but is not marked as nullable: "
                                                        + prop.getName());
                                        writer.emptyElement(
                                                WCF_DATASERVICES_NAMESPACE,
                                                prop.getName());
                                    }
                                }
                                break;
                            }
                        }
                    }
                };

                if (type.isBlob()) {
                    result = new Entry() {
                        @Override
                        public void writeInlineContent(XmlWriter writer)
                                throws SAXException {
                            try {
                                r.write(writer);
                            } catch (IOException e) {
                                throw new SAXException(e);
                            }
                        }
                    };
                    Link editLink = new Link(getValueEditRef(entity),
                            Relation.EDIT_MEDIA, null);
                    result.getLinks().add(editLink);
                    Content content = new Content();
                    // Get the external blob reference
                    content.setExternalRef(getValueRef(entity));
                    content.setToEncode(false);
                    result.setContent(content);
                } else {
                    result = new Entry();
                    Content content = new Content();
                    content.setInlineContent(r);
                    content.setToEncode(false);

                    result.setContent(content);
                }
            }
        }

        return result;
View Full Code Here

     */
    public void updateEntity(Object entity) throws Exception {
        if (getMetadata() == null || entity == null) {
            return;
        }
        Entry entry = toEntry(entity);

        ClientResource resource = createResource(getSubpath(entity));

        try {
            // TODO Fix chunked request with net client connector
            ByteArrayOutputStream o = new ByteArrayOutputStream();
            entry.write(o);
            StringRepresentation r = new StringRepresentation(o.toString(),
                    MediaType.APPLICATION_ATOM);
            String tag = getTag(entity);
            if (tag != null) {
                // Add a condition
View Full Code Here

                case TYPE_ENTITY:
                    EntryContentHandler<T> entryContentHandler = new EntryContentHandler<T>(
                            entityClass, entityType, metadata, getLogger());
                    Feed feed = new Feed();
                    feed.getEntries().add(
                            new Entry(result, entryContentHandler));
                    setFeed(feed);
                    entities = new ArrayList<T>();
                    if (entryContentHandler.getEntity() != null) {
                        entities.add(entryContentHandler.getEntity());
                    }
                    break;
                case TYPE_UNKNOWN:
                    // Guess the type of query based on the returned
                    // representation
                    Representation rep = new StringRepresentation(result
                            .getText());
                    String string = rep.getText().substring(0,
                            Math.min(100, rep.getText().length()));
                    if (string.contains("<feed")) {
                        feedContentHandler = new FeedContentHandler<T>(
                                entityClass, entityType, metadata, getLogger());
                        setFeed(new Feed(rep, feedContentHandler));
                        this.count = feedContentHandler.getCount();
                        this.entities = feedContentHandler.getEntities();
                    } else if (string.contains("<entry")) {
                        entryContentHandler = new EntryContentHandler<T>(
                                entityClass, entityType, metadata, getLogger());
                        feed = new Feed();
                        feed.getEntries().add(
                                new Entry(rep, entryContentHandler));
                        setFeed(feed);
                        entities = new ArrayList<T>();
                        if (entryContentHandler.getEntity() != null) {
                            entities.add(entryContentHandler.getEntity());
                        }
View Full Code Here

                        inlineFeedHandler.startLink(link);
                    } else {
                        inlineEntryHandler.startLink(link);
                    }
                } else if (localName.equalsIgnoreCase("entry")) {
                    Entry entry = new Entry();
                    if (association.isToMany()) {
                        inlineFeedHandler.startEntry(entry);
                    } else {
                        inlineEntryHandler.startEntry(entry);
                    }
View Full Code Here

            feed.setTitle( new Text( MediaType.TEXT_PLAIN, "All entities" ) );
            List<Entry> entries = feed.getEntries();
            final Iterable<EntityReference> query = entityFinder.findEntities( EntityComposite.class, null, null, null, null, Collections.<String, Object>emptyMap() );
            for( EntityReference entityReference : query )
            {
                Entry entry = new Entry();
                entry.setTitle( new Text( MediaType.TEXT_PLAIN, entityReference.toString() ) );
                Link link = new Link();
                link.setHref( getRequest().getResourceRef().clone().addSegment( entityReference.identity() ) );
                entry.getLinks().add( link );
                entries.add( entry );
            }

            return feed;
        }
View Full Code Here

  public Feed getFeed(String repository, int count) {
    Feed feed = getConfiguredFeed();
    List<SearchResult> results = app.getRecentArtifacts(repository, 1, count);
    for (SearchResult result : results) {
      Entry entry = toEntry(result);
      feed.getEntries().add(entry);
    }
    feed.setUpdated(new Date());
    return feed;
  }
View Full Code Here

    feed.setTitle(new Text(MediaType.TEXT_PLAIN, title));
    return feed;
  }
 
  private Entry toEntry(SearchResult result) {
    Entry entry = new Entry();
    entry.getCategories().add(newCategory(result.repository));
    entry.setContent(getContent(result.getDescription()));
    entry.setPublished(result.date);
    entry.setTitle(new Text(MediaType.TEXT_PLAIN, result.getCoordinates()));
    entry.setSummary(result.getName());
    entry.getLinks().add(newLink(result.getPath()));
    return entry;
  }
View Full Code Here

TOP

Related Classes of org.restlet.ext.atom.Entry

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.