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.internal; 014 015import static org.apache.juneau.internal.StringUtils.*; 016 017import java.lang.reflect.*; 018 019/** 020 * Utilities. 021 */ 022public class HttpUtils { 023 024 /** 025 * Given a method name, infers the REST method name. 026 * 027 * @param m The Java method. 028 * @param detectMethod Whether we should auto-detect the HTTP method name from the Java method name. 029 * @param def The default HTTP method if not detected. 030 * @return The REST method name, or the default value if not found. 031 */ 032 public static String detectHttpMethod(Method m, boolean detectMethod, String def) { 033 String n = m.getName(); 034 if (detectMethod) { 035 if (n.startsWith("do") && n.length() > 2) { 036 String n2 = n.substring(2).toUpperCase(); 037 if (isOneOf(n2, "GET","PUT","POST","DELETE","OPTIONS","HEAD","CONNECT","TRACE","PATCH")) 038 return n2; 039 } 040 for (String t : new String[]{"get","put","post","delete","options","head","connect","trace","patch"}) 041 if (n.startsWith(t) && (n.length() == t.length() || Character.isUpperCase(n.charAt(t.length())))) 042 return t.toUpperCase(); 043 } 044 return def; 045 } 046 047 /** 048 * Given a Java method, infers the REST path. 049 * 050 * @param m The Java method. 051 * @param detectMethod Whether we should auto-detect the HTTP method name from the Java method name. 052 * @return The REST path or <jk>null</jk> if not detected. 053 */ 054 public static String detectHttpPath(Method m, boolean detectMethod) { 055 String n = m.getName(); 056 if (detectMethod) { 057 if (n.startsWith("do") && n.length() > 2) { 058 String n2 = n.substring(2).toUpperCase(); 059 if (isOneOf(n2, "GET","PUT","POST","DELETE","OPTIONS","HEAD","CONNECT","TRACE","PATCH")) 060 return "/"; 061 } 062 for (String t : new String[]{"get","put","post","delete","options","head","connect","trace","patch"}) { 063 if (n.startsWith(t) && (n.length() == t.length() || Character.isUpperCase(n.charAt(t.length())))) { 064 return '/' + java.beans.Introspector.decapitalize(n.substring(t.length())); 065 } 066 } 067 } 068 return '/' + n; 069 } 070 071 /** 072 * Given a Java method, returns the arguments signature. 073 * 074 * @param m The Java method. 075 * @param full Whether fully-qualified names should be used for arguments. 076 * @return The arguments signature for the specified method. 077 */ 078 public static String getMethodArgsSignature(Method m, boolean full) { 079 StringBuilder sb = new StringBuilder(); 080 Class<?>[] pt = m.getParameterTypes(); 081 if (pt.length == 0) 082 return ""; 083 sb.append('('); 084 for (int i = 0; i < pt.length; i++) { 085 if (i > 0) 086 sb.append(','); 087 sb.append(full ? ClassUtils.getReadableClassName(pt[i]) : ClassUtils.getSimpleName(pt[i])); 088 } 089 sb.append(')'); 090 return sb.toString(); 091 } 092}