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