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