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.rest.widget;
014
015import static org.apache.juneau.dto.html5.HtmlBuilder.*;
016
017import java.net.*;
018import java.util.*;
019
020import org.apache.juneau.dto.html5.*;
021import org.apache.juneau.http.*;
022import org.apache.juneau.rest.*;
023import org.apache.juneau.serializer.*;
024import org.apache.juneau.utils.*;
025
026/**
027 * Widget that returns back a list of hyperlinks for rendering the contents of a page in a variety of content types.
028 *
029 * <p>
030 * The variable it resolves is <js>"$W{ContentTypeMenuItem}"</js>.
031 *
032 * <p>
033 * An example of this widget can be found in the <code>PetStoreResource</code> in the examples that provides
034 * a drop-down menu item for rendering all other supported content types in plain text:
035 * <p class='bcode w800'>
036 *    <ja>@RestMethod</ja>(
037 *       name=<jsf>GET</jsf>,
038 *       path=<js>"/"</js>,
039 *       widgets={
040 *          ContentTypeMenuItem.<jk>class</jk>,
041 *       },
042 *       htmldoc=<ja>@HtmlDoc</ja>(
043 *          navlinks={
044 *             <js>"up: ..."</js>,
045 *             <js>"options: ..."</js>,
046 *             <js>"$W{QueryMenuItem}"</js>,
047 *             <js>"$W{ContentTypeMenuItem}"</js>,
048 *             <js>"$W{ThemeMenuItem}"</js>,
049 *             <js>"source: ..."</js>
050 *          }
051 *       )
052 *    )
053 *    <jk>public</jk> Collection&lt;Pet&gt; getPets() {
054 * </p>
055 *
056 * <h5 class='section'>See Also:</h5>
057 * <ul>
058 *    <li class='link'>{@doc juneau-rest-server.HtmlDocAnnotation.PredefinedWidgets}
059 * </ul>
060 */
061public class ContentTypeMenuItem extends MenuItemWidget {
062
063   @Override /* MenuItemWidget */
064   public String getLabel(RestRequest req) {
065      return "content-type";
066   }
067
068   @Override /* MenuItemWidget */
069   public Div getContent(RestRequest req) {
070      Div div = div();
071      Set<MediaType> l = new TreeSet<>();
072      for (Serializer s : req.getSerializers().getSerializers())
073         l.add(s.getPrimaryMediaType());
074      for (MediaType mt : l) {
075         URI uri = req.getUri(true, new AMap<String,String>().append("plainText","true").append("Accept",mt.toString()));
076         div.children(a(uri, mt), br());
077      }
078      return div;
079   }
080}