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.internal; 018 019import java.lang.reflect.*; 020 021/** 022 * HTTP utilities. 023 * 024 * <h5 class='section'>See Also:</h5><ul> 025 * </ul> 026 */ 027public class HttpUtils { 028 029 private static final String[] 030 LC_METHODS = {"get","put","post","delete","options","head","connect","trace","patch"}, 031 UC_METHODS = {"GET","PUT","POST","DELETE","OPTIONS","HEAD","CONNECT","TRACE","PATCH"}; 032 033 /** 034 * Given a method name, infers the REST method name. 035 * 036 * @param m The Java method. 037 * @param detectMethod Whether we should auto-detect the HTTP method name from the Java method name. 038 * @param def The default HTTP method if not detected. 039 * @return The REST method name, or the default value if not found. 040 */ 041 public static String detectHttpMethod(Method m, boolean detectMethod, String def) { 042 String n = m.getName(); 043 if (detectMethod) { 044 if (n.startsWith("do") && n.length() > 2) { 045 String n2 = n.substring(2).toUpperCase(); 046 for (String t : UC_METHODS) 047 if (n2.equals(t)) 048 return n2; 049 } 050 for (String t : LC_METHODS) 051 if (n.startsWith(t) && (n.length() == t.length() || Character.isUpperCase(n.charAt(t.length())))) 052 return t.toUpperCase(); 053 } 054 return def; 055 } 056 057 /** 058 * Given a Java method, infers the REST path. 059 * 060 * @param m The Java method. 061 * @param method The HTTP method name if it's known. 062 * @return The REST path or <jk>null</jk> if not detected. 063 */ 064 public static String detectHttpPath(Method m, String method) { 065 String n = m.getName(); 066 if (method == null) { 067 if (n.startsWith("do") && n.length() > 2) { 068 String n2 = n.substring(2).toUpperCase(); 069 for (String t : UC_METHODS) 070 if (n2.equals(t)) 071 return "/"; 072 } 073 for (String t : LC_METHODS) { 074 if (n.startsWith(t) && (n.length() == t.length() || Character.isUpperCase(n.charAt(t.length())))) { 075 return '/' + java.beans.Introspector.decapitalize(n.substring(t.length())); 076 } 077 } 078 } else { 079 if (n.equalsIgnoreCase(method) || n.equals("do") || n.equals("_")) 080 return "/"; 081 if (n.startsWith(method) && (n.length() == method.length() || Character.isUpperCase(n.charAt(method.length())))) { 082 return '/' + java.beans.Introspector.decapitalize(n.substring(method.length())); 083 } 084 } 085 return '/' + n; 086 } 087}