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