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