001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.rest.converter;
018
019import org.apache.juneau.*;
020import org.apache.juneau.http.response.*;
021import org.apache.juneau.json.*;
022import org.apache.juneau.objecttools.*;
023import org.apache.juneau.rest.*;
024import org.apache.juneau.swap.*;
025
026/**
027 * Converter for enablement of {@link ObjectIntrospector} support on response objects returned by a
028 * <c>@RestOp</c>-annotated method.
029 *
030 * <p>
031 * When enabled, public methods can be called on objects returned through the {@link RestResponse#setContent(Object)}
032 * method.
033 *
034 * <p>
035 * Note that opening up public methods for calling through a REST interface can be dangerous, and should be done with
036 * caution.
037 *
038 * <p>
039 * Java methods are invoked by passing in the following URL parameters:
040 * <ul class='spaced-list'>
041 *    <li>
042 *       <c>&amp;invokeMethod</c> - The Java method name, optionally with arguments if necessary to
043 *       differentiate between methods.
044 *    <li>
045 *       <c>&amp;invokeArgs</c> - The arguments as an array.
046 * </ul>
047 *
048 * <h5 class='section'>See Also:</h5><ul>
049 *    <li class='jc'>{@link ObjectIntrospector} - Additional information on introspection of POJO methods.
050 *    <li class='jm'>{@link org.apache.juneau.rest.RestOpContext.Builder#converters()} - Registering converters with REST resources.
051 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/Converters">Converters</a>
052 * </ul>
053 */
054public class Introspectable implements RestConverter {
055
056   /**
057    * Swagger parameters for this converter.
058    */
059   public static final String SWAGGER_PARAMS= ""
060      + "{in:'query',name:'invokeMethod',description:' The Java method name, optionally with arguments if necessary to differentiate between methods.',examples:{example:'toString'}},"
061      + "{in:'query',name:'invokeArgs',description:'The arguments as an array.',examples:{example:'foo,bar'}}"
062   ;
063
064   @Override /* RestConverter */
065   @SuppressWarnings({"unchecked", "rawtypes"})
066   public Object convert(RestRequest req, Object o) throws InternalServerError {
067      String method = req.getQueryParam("invokeMethod").orElse(null);
068      String args = req.getQueryParam("invokeArgs").orElse(null);
069      if (method == null)
070         return o;
071      try {
072         BeanSession bs = req.getBeanSession();
073         ObjectSwap swap = bs.getClassMetaForObject(o).getSwap(bs);
074         if (swap != null)
075            o = swap.swap(bs, o);
076         return ObjectIntrospector.create(o, JsonParser.DEFAULT).invokeMethod(method, args);
077      } catch (Exception e) {
078         return new InternalServerError(e,
079            "Error occurred trying to invoke method: {0}",
080            e.getLocalizedMessage()
081         );
082      }
083   }
084}