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