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 <c>PetStoreResource</c> 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<Pet> getPets() { 054 * </p> 055 * 056 * <ul class='seealso'> 057 * <li class='link'>{@doc juneau-rest-server.HtmlDocAnnotation.PredefinedWidgets} 058 * </ul> 059 */ 060public class ContentTypeMenuItem extends MenuItemWidget { 061 062 @Override /* MenuItemWidget */ 063 public String getLabel(RestRequest req, RestResponse res) { 064 return "content-type"; 065 } 066 067 @Override /* MenuItemWidget */ 068 public Div getContent(RestRequest req, RestResponse res) { 069 Div div = div(); 070 Set<MediaType> l = new TreeSet<>(); 071 for (Serializer s : req.getSerializers().getSerializers()) 072 l.add(s.getPrimaryMediaType()); 073 for (MediaType mt : l) { 074 URI uri = req.getUri(true, new AMap<String,String>().append("plainText","true").append("Accept",mt.toString())); 075 div.children(a(uri, mt), br()); 076 } 077 return div; 078 } 079}