You are viewing a plain text version of this content. The canonical link for it is here.
Posted to wsrp4j-dev@portals.apache.org by dl...@apache.org on 2005/08/25 18:25:06 UTC

cvs commit: ws-wsrp4j/sandbox/wsrp4j/commons/src/java/org/apache/wsrp4j/commons/util LocaleHelper.java AuthenticationInfoHelper.java

dlouzan     2005/08/25 09:25:06

  Added:       sandbox/wsrp4j/commons/src/java/org/apache/wsrp4j/commons/util
                        LocaleHelper.java AuthenticationInfoHelper.java
  Log:
  Initial commit.
  
  Revision  Changes    Path
  1.1                  ws-wsrp4j/sandbox/wsrp4j/commons/src/java/org/apache/wsrp4j/commons/util/LocaleHelper.java
  
  Index: LocaleHelper.java
  ===================================================================
  /*
   * Copyright 2003-2005 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package org.apache.wsrp4j.commons.util;
  
  import java.util.Locale;
  import java.util.ArrayList;
  
  /**
   * @version $Id: LocaleHelper.java,v 1.1 2005/08/25 16:25:05 dlouzan Exp $
   */
  public class LocaleHelper {
      
      private final static String LOCALE_SEPARATOR = "-";
      
      /**
       * Gets the language code from a locales string representation, assuming
       * that the provided locale has a format like 
       * [2 char language code]"-"[2 char country code].
       *
       * @param locale The locale as <code>String</code>.
       * @return The language code.
       **/
      public static String getLanguageCode(String locale) {
          
          String code = "";
          
          int pos = locale.indexOf(LOCALE_SEPARATOR);
          
          if (pos != -1) {
              code = locale.substring(0, pos);
              
          } else if (locale.length() == 2) {
              //see if locale is language only
              code = locale;
              
          } else if ((pos = locale.indexOf("_")) != -1) {
              
              // see if separator is "_" instead of "-"
              code = locale.substring(0, pos);
              
          }
          
          return code;
      }
      
      /**
       * Gets the country code from a locales string representation, assuming
       * that the provided locale has a format like 
       * [2 char language code]"-"[2 char country code].
       *
       * @param locale The locale as <code>String</code>.
       * @return The country code.
       **/
      public static String getCountryCode(String locale) {
          
          String code = "";
          
          int pos = locale.indexOf(LOCALE_SEPARATOR);
          
          if (pos != -1) {
              code = locale.substring(pos + 1, locale.length());
              
          } else if ((pos = locale.indexOf("_")) != -1) {
              
              // see if separator is "_" instead of "-"
              code = locale.substring(0, pos);
          }
          
          return code;
      }
      
      /**
       * Convinence method to create a <code>Locale</code> object from a locales
       * string representation, assuming that the provided locale has a format 
       * like [2 char language code]"-"[2 char country code].
       *
       * @param locale The locale as <code>String</code>.
       * @return The corresponding <code>Locale</code> object.
       **/
      public static Locale getLocale(String locale) {
          return new Locale(getLanguageCode(locale), getCountryCode(locale));
      }
      
      /**
       * The method takes a given <code>Locale</code> and returns a string
       * representation which is conform to the WSRP standard.
       *
       * @param locale The <code>Locale</code> to be formatted.
       * @return A string representation of the given locale which is conform to 
       * the WSRP standard.
       **/
      public static String getWsrpLocale(Locale locale) {
          if(locale == null) return null;
          
          String lang = locale.getLanguage();
          String country = locale.getCountry();
          
          return lang + ((country != null && !country.equals("")) ? 
              "-" + country : "");
      }
      
      /**
       * Convinence method to create a <code>Locale</code> object array from a 
       * locales string array representation, assuming that the provided locale 
       * has a format like [2 char language code]"-"[2 char country code].
       *
       * @param locales The locale as <code>String</code>.
       * @return The corresponding <code>Locale</code> object.
       **/
      public static Locale[] getLocales(String[] locales) {
          
          ArrayList list = new ArrayList();
          
          for (int i=0; i<locales.length; i++) {
              list.add(getLocale(locales[i]));
          }
          
          Locale[] typedArray = new Locale[locales.length];
          
          return (Locale[]) list.toArray(typedArray);
      }
      
  }
  
  
  
  1.1                  ws-wsrp4j/sandbox/wsrp4j/commons/src/java/org/apache/wsrp4j/commons/util/AuthenticationInfoHelper.java
  
  Index: AuthenticationInfoHelper.java
  ===================================================================
  /*
   * Copyright 2003-2005 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package org.apache.wsrp4j.commons.util;
  
  import javax.portlet.PortletRequest;
  
  
  /**
   * @version $Id: AuthenticationInfoHelper.java,v 1.1 2005/08/25 16:25:05 dlouzan Exp $
   */
  public class AuthenticationInfoHelper {
      
      /**
       * No authentication was done
       **/
      public static final String WSRP_NONE = "wsrp:none";
      
      /**
       * End-User identified themselves using password/username scenario
       **/
      public static final String WSRP_PASSWD = "wsrp:password";
      
      /**
       * End-User presented a security certificate
       **/
      public static final String WSRP_CERT = "wsrp:certificate";
      
      /**
       * Get a string representation of the user authentification
       * as defined in the WSRP spec. from a passed authentification
       * info defined in the portlet spec. If the passed value could not
       * be matched the same string is returned.
       *
       * @param jsrAuthInfo Authentification info as defined in the portlet spec
       * @return The authentification info as defined in the WSRP spec. or the
       *          argument if no match could be made.
       **/
      public static String getWsrpFromPortlet(String jsrAuthInfo) {
          if (jsrAuthInfo == null) {
              return WSRP_NONE;
          } else if (jsrAuthInfo == PortletRequest.BASIC_AUTH
                  || jsrAuthInfo == PortletRequest.FORM_AUTH
                  || jsrAuthInfo.equals(PortletRequest.BASIC_AUTH)
                  || jsrAuthInfo.equals(PortletRequest.FORM_AUTH)) {
              return WSRP_PASSWD;
              
          } else if (
                  jsrAuthInfo == PortletRequest.CLIENT_CERT_AUTH || 
                  jsrAuthInfo.equals(PortletRequest.CLIENT_CERT_AUTH)) {
              
              return WSRP_CERT;
              
          } else {
              
              return jsrAuthInfo;
          }
      }
      
      /**
       * Get the authentification info as defined in the portlet spec
       * from a passed authentification info defined in the WSRP spec..
       * If wsrp:none is passed <code>null</code> is returned. In case the
       * passed info could not be matched the same string is returned.
       *
       * @param wsrpInfo
       **/
      public static String getPortletFromWsrp(String wsrpInfo) {
          if (wsrpInfo.equals(WSRP_PASSWD)) {
              return PortletRequest.FORM_AUTH;
              
          } else if (wsrpInfo.equals(WSRP_CERT)) {
              return PortletRequest.CLIENT_CERT_AUTH;
              
          } else if (wsrpInfo.equals(WSRP_NONE)) {
              
              return null;
              
          } else {
              
              return wsrpInfo;
          }
      }
      
  }