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 javax.servlet.http.HttpServletResponse.*; 016 017import org.apache.juneau.*; 018import org.apache.juneau.json.*; 019import org.apache.juneau.rest.*; 020import org.apache.juneau.transform.*; 021import org.apache.juneau.utils.*; 022 023/** 024 * Converter for enablement of {@link PojoIntrospector} support on response objects returned by a 025 * <code>@RestMethod</code> method. 026 * 027 * <p> 028 * When enabled, public methods can be called on objects returned through the {@link RestResponse#setOutput(Object)} 029 * method. 030 * 031 * <p> 032 * Note that opening up public methods for calling through a REST interface can be dangerous, and should be done with 033 * caution. 034 * 035 * <p> 036 * Java methods are invoked by passing in the following URL parameters: 037 * <ul class='spaced-list'> 038 * <li> 039 * <code>&invokeMethod</code> - The Java method name, optionally with arguments if necessary to 040 * differentiate between methods. 041 * <li> 042 * <code>&invokeArgs</code> - The arguments as a JSON array. 043 * </ul> 044 * 045 * <h5 class='section'>See Also:</h5> 046 * <ul> 047 * <li class='jc'>{@link PojoIntrospector} - Additional information on introspection of POJO methods. 048 * <li class='jf'>{@link RestContext#REST_converters} - Registering converters with REST resources. 049 * <li class='link'><a class="doclink" href="../../../../../overview-summary.html#juneau-rest-server.Converters">Overview > juneau-rest-server > Converters</a> 050 * </ul> 051 */ 052public final class Introspectable implements RestConverter { 053 054 @Override /* RestConverter */ 055 @SuppressWarnings({"unchecked", "rawtypes"}) 056 public Object convert(RestRequest req, Object o) throws RestException { 057 String method = req.getQuery().getString("invokeMethod"); 058 String args = req.getQuery().getString("invokeArgs"); 059 if (method == null) 060 return o; 061 try { 062 BeanSession bs = req.getBeanSession(); 063 PojoSwap swap = bs.getClassMetaForObject(o).getPojoSwap(bs); 064 if (swap != null) 065 o = swap.swap(bs, o); 066 return new PojoIntrospector(o, JsonParser.DEFAULT).invokeMethod(method, args); 067 } catch (Exception e) { 068 e.printStackTrace(); 069 return new RestException(SC_INTERNAL_SERVER_ERROR, 070 "Error occurred trying to invoke method: {0}", 071 e.getLocalizedMessage() 072 ).initCause(e); 073 } 074 } 075}