001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.examples.rest.dto;
014
015import static org.apache.juneau.dto.atom.AtomBuilder.*;
016
017import java.net.URI;
018
019import org.apache.juneau.annotation.*;
020import org.apache.juneau.dto.atom.*;
021import org.apache.juneau.encoders.*;
022import org.apache.juneau.html.annotation.*;
023import org.apache.juneau.http.annotation.*;
024import org.apache.juneau.http.annotation.Content;
025import org.apache.juneau.rest.annotation.*;
026import org.apache.juneau.rest.servlet.*;
027import org.apache.juneau.rest.widget.*;
028import org.apache.juneau.serializer.annotation.*;
029
030/**
031 * Sample resource that shows how to generate ATOM feeds.
032 *
033 * <h5 class='section'>See Also:</h5><ul>
034
035 * </ul>
036 *
037 * @serial exclude
038 */
039@Rest(
040   path="/atom",
041   title="Sample ATOM feed resource",
042   description="Sample resource that shows how to render ATOM feeds",
043   encoders=GzipEncoder.class,
044   swagger=@Swagger(
045      contact=@Contact(name="Juneau Developer",email="dev@juneau.apache.org"),
046      license=@License(name="Apache 2.0",url="http://www.apache.org/licenses/LICENSE-2.0.html"),
047      version="2.0",
048      termsOfService="You are on your own.",
049      externalDocs=@ExternalDocs(description="Apache Juneau",url="http://juneau.apache.org")
050   )
051)
052@HtmlDocConfig(
053   widgets={
054      ContentTypeMenuItem.class
055   },
056   navlinks={
057      "up: request:/..",
058      "api: servlet:/api",
059      "stats: servlet:/stats",
060      "$W{ContentTypeMenuItem}",
061      "source: $C{Source/gitHub}/org/apache/juneau/examples/rest/dto/AtomFeedResource.java"
062   }
063)
064@SerializerConfig(
065   quoteChar="'"
066)
067public class AtomFeedResource extends BasicRestServlet {
068   private static final long serialVersionUID = 1L;
069
070   private Feed feed;     // The root resource object
071
072   @Override /* Servlet */
073   public void init() {
074      try {
075         feed =
076            feed("tag:foo.org", "Title", "2016-12-31T05:02:03Z")
077            .setSubtitle(text("html").setText("Subtitle"))
078            .setLinks(
079               link("alternate", "text/html", "http://foo.org/").setHreflang("en"),
080               link("self", "application/atom+xml", "http://foo.org/feed.atom")
081            )
082            .setGenerator(
083               generator("Example Toolkit").setUri("http://www.foo.org/").setVersion("1.0")
084            )
085            .setEntries(
086               entry("tag:foo.org", "Title", "2016-12-31T05:02:03Z")
087               .setLinks(
088                  link("alternate", "text/html", "http://foo.org/2005/04/02/atom"),
089                  link("enclosure", "audio/mpeg", "http://foo.org/audio/foobar.mp3").setLength(1337)
090               )
091               .setPublished("2016-12-31T05:02:03Z")
092               .setAuthors(
093                  person("John Smith").setUri(new URI("http://foo.org/")).setEmail("foo@foo.org")
094               )
095               .setContributors(
096                  person("John Smith"),
097                  person("Jane Smith")
098               )
099               .setContent(
100                  content("xhtml")
101                  .setLang("en")
102                  .setBase("http://foo.org/")
103                  .setText("<div><p><i>[Sample content]</i></p></div>")
104               )
105            );
106      } catch (Exception e) {
107         throw new IllegalStateException(e);
108      }
109   }
110
111   /**
112    * [HTTP GET /dto/atom]
113    * Get the sample ATOM feed
114    *
115    * @return The sample ATOM feed.
116    */
117   @RestGet(
118      summary="Get the sample ATOM feed"
119   )
120   public Feed get() {
121      return feed;
122   }
123
124   /**
125    * [HTTP PUT /dto/atom]
126    * Overwrite the sample ATOM feed
127    *
128    * @param feed The new ATOM feed.
129    * @return The updated ATOM feed.
130    */
131   @RestPut(
132      summary="Overwrite the sample ATOM feed",
133      description="Replaces the feed with the specified content, and then mirrors it as the response."
134   )
135   public Feed put(@Content Feed feed) {
136      this.feed = feed;
137      return feed;
138   }
139}