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.rrpc; 014 015import static org.apache.juneau.common.internal.StringUtils.*; 016 017import java.io.*; 018import java.lang.reflect.*; 019 020import org.apache.juneau.http.remote.*; 021import org.apache.juneau.http.response.*; 022import org.apache.juneau.parser.*; 023import org.apache.juneau.rest.*; 024 025/** 026 * A session for a single HTTP request against an RRPC Java method. 027 * 028 * <h5 class='section'>Notes:</h5><ul> 029 * <li class='warn'>This class is not thread safe. 030 * </ul> 031 * 032 * <h5 class='section'>See Also:</h5><ul> 033 * <li class='link'><a class="doclink" href="../../../../../index.html#jrs.RestRpc">REST/RPC</a> 034 * </ul> 035 */ 036public class RrpcRestOpSession extends RestOpSession { 037 038 //----------------------------------------------------------------------------------------------------------------- 039 // Static 040 //----------------------------------------------------------------------------------------------------------------- 041 042 /** 043 * Static creator. 044 * 045 * @param ctx The context of the RRPC Java Method. 046 * @param session The REST session creating this session. 047 * @return A new builder. 048 */ 049 public static Builder create(RrpcRestOpContext ctx, RestSession session) { 050 return new Builder(ctx, session); 051 052 } 053 054 //----------------------------------------------------------------------------------------------------------------- 055 // Builder 056 //----------------------------------------------------------------------------------------------------------------- 057 058 /** 059 * Builder class. 060 */ 061 public static class Builder extends RestOpSession.Builder { 062 063 RrpcRestOpContext ctx; 064 065 /** 066 * Constructor. 067 * 068 * @param ctx The context object of the RRPC Java method. 069 * @param session The REST session. 070 */ 071 public Builder(RrpcRestOpContext ctx, RestSession session) { 072 super(ctx, session); 073 this.ctx = ctx; 074 } 075 076 @Override 077 public RrpcRestOpSession build() { 078 return new RrpcRestOpSession(this); 079 } 080 081 } 082 083 //----------------------------------------------------------------------------------------------------------------- 084 // Instance 085 //----------------------------------------------------------------------------------------------------------------- 086 087 private final RrpcRestOpContext ctx; 088 089 /** 090 * Constructor. 091 * 092 * @param builder The builder for this object. 093 */ 094 protected RrpcRestOpSession(Builder builder) { 095 super(builder); 096 ctx = builder.ctx; 097 } 098 099 @Override 100 public void run() throws Throwable { 101 102 super.run(); 103 104 RestRequest req = getRequest(); 105 RestResponse res = getResponse(); 106 RestSession session = getRestSession(); 107 108 final Object o = res.hasContent() ? res.getContent(Object.class) : null; 109 110 if ("GET".equals(session.getMethod())) { 111 res.setContent(ctx.getMeta().getMethodsByPath().keySet()); 112 return; 113 114 } else if ("POST".equals(session.getMethod())) { 115 String pip = session.getUrlPath().getPath(); 116 if (pip.indexOf('/') != -1) 117 pip = pip.substring(pip.lastIndexOf('/')+1); 118 pip = urlDecode(pip); 119 RrpcInterfaceMethodMeta rmm = ctx.getMeta().getMethodMetaByPath(pip); 120 if (rmm != null) { 121 Method m = rmm.getJavaMethod(); 122 try { 123 // Parse the args and invoke the method. 124 Parser p = req.getContent().getParserMatch().get().getParser(); 125 Object[] args = null; 126 if (m.getGenericParameterTypes().length == 0) 127 args = new Object[0]; 128 else { 129 try (Closeable in = p.isReaderParser() ? req.getReader() : req.getInputStream()) { 130 args = p.parseArgs(in, m.getGenericParameterTypes()); 131 } 132 } 133 res.setContent(m.invoke(o, args)); 134 return; 135 } catch (BasicHttpException e) { 136 throw e; 137 } catch (Exception e) { 138 throw new InternalServerError(e); 139 } 140 } 141 } 142 throw new NotFound(); 143 } 144}