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 static org.apache.juneau.rest.HttpRuntimeException.*;
016
017import org.apache.juneau.*;
018import org.apache.juneau.rest.*;
019import org.apache.juneau.http.exception.*;
020import org.apache.juneau.transform.*;
021import org.apache.juneau.utils.*;
022
023/**
024 * Converter for enabling of {@link PojoRest} support on response objects returned by a <c>@RestMethod</c> method.
025 *
026 * <p>
027 * When enabled, objects in a POJO tree returned by the REST method can be addressed through additional URL path
028 * information.
029 *
030 * <p class='bcode w800'>
031 *    <jc>// Resource method on resource "http://localhost:8080/sample/addressBook"</jc>
032 *    <ja>@RestMethod</ja>(name=<jsf>GET</jsf>, converters=Traversable.<jk>class</jk>)
033 *    <jk>public void</jk> doGet(RestRequest req, RestResponse res) {
034 *       <jk>return new</jk> AddressBook();
035 *    }
036 *
037 *    <jc>// Sample usage</jc>
038 *    <jk>public static void</jk> main(String[] args) {
039 *       <jc>// Get the zip code of the 2nd address of the first person in the address book.</jc>
040 *       RestCall r = <jk>new</jk> RestClient().doGet(<js>"http://localhost:8080/sample/addressBook/0/addresses/1/zip"</js>);
041 *       <jk>int</jk> zip = r.getResponse(Integer.<jk>class</jk>);
042 *    }
043 * </p>
044 *
045 * <ul class='seealso'>
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'>{@doc RestConverters}
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 HttpException, InternalServerError {
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).getSwap(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 HttpException(e, e.getStatus());
071         } catch (Throwable t) {
072            throw toHttpException(t, InternalServerError.class);
073         }
074      }
075
076      return o;
077   }
078}