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 <code>@RestMethod</code> 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 * <h5 class='section'>See Also:</h5>
044 * <ul>
045 *    <li class='jc'>{@link PojoRest} - Additional information on addressing elements in a POJO tree using URL notation.
046 *    <li class='jf'>{@link RestContext#REST_converters} - Registering converters with REST resources.
047 *    <li class='link'>{@doc juneau-rest-server.Converters}
048 * </ul>
049 */
050public final class Traversable implements RestConverter {
051
052   @Override /* RestConverter */
053   @SuppressWarnings({"rawtypes", "unchecked"})
054   public Object convert(RestRequest req, Object o) throws RestException, InternalServerError {
055      if (o == null)
056         return null;
057
058      String pathRemainder = req.getPathMatch().getRemainder();
059
060      if (pathRemainder != null) {
061         try {
062            BeanSession bs = req.getBeanSession();
063            PojoSwap swap = bs.getClassMetaForObject(o).getPojoSwap(bs);
064            if (swap != null)
065               o = swap.swap(bs, o);
066            PojoRest p = new PojoRest(o, req.getBody().getReaderParser());
067            o = p.get(pathRemainder);
068         } catch (PojoRestException e) {
069            throw new RestException(e, e.getStatus());
070         } catch (Exception e) {
071            throw new InternalServerError(e);
072         }
073      }
074
075      return o;
076   }
077}