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.json.*; 017import org.apache.juneau.rest.*; 018import org.apache.juneau.rest.exception.*; 019import org.apache.juneau.transform.*; 020import org.apache.juneau.utils.*; 021 022/** 023 * Converter for enablement of {@link PojoIntrospector} support on response objects returned by a 024 * <code>@RestMethod</code> method. 025 * 026 * <p> 027 * When enabled, public methods can be called on objects returned through the {@link RestResponse#setOutput(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 * <code>&invokeMethod</code> - The Java method name, optionally with arguments if necessary to 039 * differentiate between methods. 040 * <li> 041 * <code>&invokeArgs</code> - The arguments as an array. 042 * </ul> 043 * 044 * <h5 class='section'>See Also:</h5> 045 * <ul> 046 * <li class='jc'>{@link PojoIntrospector} - Additional information on introspection of POJO methods. 047 * <li class='jf'>{@link RestContext#REST_converters} - Registering converters with REST resources. 048 * <li class='link'>{@doc juneau-rest-server.Converters} 049 * </ul> 050 */ 051public final class Introspectable implements RestConverter { 052 053 /** 054 * Swagger parameters for this converter. 055 */ 056 public static final String SWAGGER_PARAMS= "" 057 + "{in:'query',name:'invokeMethod',description:' The Java method name, optionally with arguments if necessary to differentiate between methods.',x-examples:{example:'toString'}}," 058 + "{in:'query',name:'invokeArgs',description:'The arguments as an array.',x-examples:{example:'foo,bar'}}" 059 ; 060 061 @Override /* RestConverter */ 062 @SuppressWarnings({"unchecked", "rawtypes"}) 063 public Object convert(RestRequest req, Object o) throws InternalServerError { 064 String method = req.getQuery().getString("invokeMethod"); 065 String args = req.getQuery().getString("invokeArgs"); 066 if (method == null) 067 return o; 068 try { 069 BeanSession bs = req.getBeanSession(); 070 PojoSwap swap = bs.getClassMetaForObject(o).getPojoSwap(bs); 071 if (swap != null) 072 o = swap.swap(bs, o); 073 return new PojoIntrospector(o, JsonParser.DEFAULT).invokeMethod(method, args); 074 } catch (Exception e) { 075 return new InternalServerError( 076 "Error occurred trying to invoke method: {0}", 077 e.getLocalizedMessage() 078 ).initCause(e); 079 } 080 } 081}