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.json.*;
017import org.apache.juneau.objecttools.*;
018import org.apache.juneau.rest.*;
019import org.apache.juneau.swap.*;
020import org.apache.juneau.http.response.*;
021
022/**
023 * Converter for enablement of {@link ObjectIntrospector} support on response objects returned by a
024 * <c>@RestOp</c>-annotated method.
025 *
026 * <p>
027 * When enabled, public methods can be called on objects returned through the {@link RestResponse#setContent(Object)}
028 * method.
029 *
030 * <p>
031 * Note that opening up public methods for calling through a REST interface can be dangerous, and should be done with
032 * caution.
033 *
034 * <p>
035 * Java methods are invoked by passing in the following URL parameters:
036 * <ul class='spaced-list'>
037 *    <li>
038 *       <c>&amp;invokeMethod</c> - The Java method name, optionally with arguments if necessary to
039 *       differentiate between methods.
040 *    <li>
041 *       <c>&amp;invokeArgs</c> - The arguments as an array.
042 * </ul>
043 *
044 * <h5 class='section'>See Also:</h5><ul>
045 *    <li class='jc'>{@link ObjectIntrospector} - Additional information on introspection of POJO methods.
046 *    <li class='jm'>{@link org.apache.juneau.rest.RestOpContext.Builder#converters()} - Registering converters with REST resources.
047 *    <li class='link'><a class="doclink" href="../../../../../index.html#jrs.Converters">Converters</a>
048 * </ul>
049 */
050public final class Introspectable implements RestConverter {
051
052   /**
053    * Swagger parameters for this converter.
054    */
055   public static final String SWAGGER_PARAMS= ""
056      + "{in:'query',name:'invokeMethod',description:' The Java method name, optionally with arguments if necessary to differentiate between methods.',examples:{example:'toString'}},"
057      + "{in:'query',name:'invokeArgs',description:'The arguments as an array.',examples:{example:'foo,bar'}}"
058   ;
059
060   @Override /* RestConverter */
061   @SuppressWarnings({"unchecked", "rawtypes"})
062   public Object convert(RestRequest req, Object o) throws InternalServerError {
063      String method = req.getQueryParam("invokeMethod").orElse(null);
064      String args = req.getQueryParam("invokeArgs").orElse(null);
065      if (method == null)
066         return o;
067      try {
068         BeanSession bs = req.getBeanSession();
069         ObjectSwap swap = bs.getClassMetaForObject(o).getSwap(bs);
070         if (swap != null)
071            o = swap.swap(bs, o);
072         return ObjectIntrospector.create(o, JsonParser.DEFAULT).invokeMethod(method, args);
073      } catch (Exception e) {
074         return new InternalServerError(e,
075            "Error occurred trying to invoke method: {0}",
076            e.getLocalizedMessage()
077         );
078      }
079   }
080}