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