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.converter;
014
015import org.apache.juneau.*;
016import org.apache.juneau.rest.*;
017import org.apache.juneau.swap.*;
018import org.apache.juneau.http.response.*;
019import org.apache.juneau.objecttools.*;
020import org.apache.juneau.parser.*;
021
022/**
023 * Converter for enabling of {@link ObjectRest} support on response objects returned by a <c>@RestOp</c>-annotated 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='bjava'>
030 *    <jc>// Resource method on resource "http://localhost:8080/sample/addressBook"</jc>
031 *    <ja>@RestOp</ja>(method=<jsf>GET</jsf>, converters=Traversable.<jk>class</jk>)
032 *    <jk>public void</jk> doGet(RestRequest <jv>req</jv>, RestResponse <jv>res</jv>) {
033 *       <jk>return new</jk> AddressBook();
034 *    }
035 * </p>
036 *
037 * <h5 class='section'>See Also:</h5><ul>
038 *    <li class='jc'>{@link ObjectRest} - Additional information on addressing elements in a POJO tree using URL notation.
039 *    <li class='jm'>{@link org.apache.juneau.rest.RestOpContext.Builder#converters()} - Registering converters with REST resources.
040 *    <li class='link'><a class="doclink" href="../../../../../index.html#jrs.Converters">Converters</a>
041 * </ul>
042 */
043public final class Traversable implements RestConverter {
044
045   @Override /* RestConverter */
046   @SuppressWarnings({"rawtypes", "unchecked"})
047   public Object convert(RestRequest req, Object o) throws BasicHttpException, InternalServerError {
048      if (o == null)
049         return null;
050
051      String pathRemainder = req.getPathRemainder().orElse(null);
052
053      if (pathRemainder != null) {
054         try {
055            BeanSession bs = req.getBeanSession();
056            ObjectSwap swap = bs.getClassMetaForObject(o).getSwap(bs);
057            if (swap != null)
058               o = swap.swap(bs, o);
059            ReaderParser rp = req.getContent().getParserMatch().map(ParserMatch::getParser).filter(ReaderParser.class::isInstance).map(ReaderParser.class::cast).orElse(null);
060            ObjectRest or = ObjectRest.create(o, rp);
061            o = or.get(pathRemainder);
062         } catch (ObjectRestException e) {
063            throw new BasicHttpException(e.getStatus(), e);
064         } catch (Throwable t) {
065            throw new InternalServerError(t);
066         }
067      }
068
069      return o;
070   }
071}