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.converters; 014 015import javax.servlet.http.*; 016 017import org.apache.juneau.*; 018import org.apache.juneau.rest.*; 019import org.apache.juneau.transform.*; 020import org.apache.juneau.utils.*; 021 022/** 023 * Converter for enabling of {@link PojoRest} support on response objects returned by a <code>@RestMethod</code> method. 024 * 025 * <p> 026 * When enabled, objects in a POJO tree returned by the REST method can be addressed through additional URL path 027 * information. 028 * 029 * <p class='bcode'> 030 * <jc>// Resource method on resource "http://localhost:8080/sample/addressBook"</jc> 031 * <ja>@RestMethod</ja>(name=<jsf>GET</jsf>, converters=Traversable.<jk>class</jk>) 032 * <jk>public void</jk> doGet(RestRequest req, RestResponse res) { 033 * <jk>return new</jk> AddressBook(); 034 * } 035 * 036 * <jc>// Sample usage</jc> 037 * <jk>public static void</jk> main(String[] args) { 038 * <jc>// Get the zip code of the 2nd address of the first person in the address book.</jc> 039 * RestCall r = <jk>new</jk> RestClient().doGet(<js>"http://localhost:8080/sample/addressBook/0/addresses/1/zip"</js>); 040 * <jk>int</jk> zip = r.getResponse(Integer.<jk>class</jk>); 041 * } 042 * </p> 043 * 044 * <h5 class='section'>See Also:</h5> 045 * <ul> 046 * <li class='jc'>{@link PojoRest} - Additional information on addressing elements in a POJO tree using URL notation. 047 * <li class='jf'>{@link RestContext#REST_converters} - Registering converters with REST resources. 048 * <li class='link'><a class="doclink" href="../../../../../overview-summary.html#juneau-rest-server.Converters">Overview > juneau-rest-server > Converters</a> 049 * </ul> 050 */ 051public final class Traversable implements RestConverter { 052 053 @Override /* RestConverter */ 054 @SuppressWarnings({"rawtypes", "unchecked"}) 055 public Object convert(RestRequest req, Object o) throws RestException { 056 if (o == null) 057 return null; 058 059 String pathRemainder = req.getPathMatch().getRemainder(); 060 061 if (pathRemainder != null) { 062 try { 063 BeanSession bs = req.getBeanSession(); 064 PojoSwap swap = bs.getClassMetaForObject(o).getPojoSwap(bs); 065 if (swap != null) 066 o = swap.swap(bs, o); 067 PojoRest p = new PojoRest(o, req.getBody().getReaderParser()); 068 o = p.get(pathRemainder); 069 } catch (PojoRestException e) { 070 throw new RestException(e.getStatus(), e); 071 } catch (Exception e) { 072 throw new RestException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e); 073 } 074 } 075 076 return o; 077 } 078}