Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2014 Philip Helger (www.helger.com) philip[at]helger[dot]com 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 com.helger.webbasics.userdata;
 
 import java.io.File;
 
 
Manager for UserDataObject objects.

Author(s):
Philip Helger
 
 public final class UserDataManager
 {
  
The default user data path, relative to a URL context and the servlet context directory.
 
   public static final String DEFAULT_USER_DATA_PATH = "/user";
  
By default the user data is accessed via the servletContext IO
 
   public static final boolean DEFAULT_SERVLET_CONTEXT_IO = true;
 
   private static final ReadWriteLock s_aRWLock = new ReentrantReadWriteLock ();
   private static String s_sUserDataPath = ;
   private static boolean s_bServletContextIO = ;
 
   private UserDataManager ()
   {}

  
Set the user data path, relative to the URL context and relative to the servlet context directory.

Parameters:
sUserDataPath The path to be set. May neither be null nor empty and must start with a "/" character.
 
   public static void setUserDataPath (@Nonnull @Nonempty final String sUserDataPath)
   {
     if (StringHelper.getLength (sUserDataPath) < 2)
       throw new IllegalArgumentException ("userDataPath is too short");
     if (!sUserDataPath.startsWith ("/"))
       throw new IllegalArgumentException ("userDataPath must start with a slash");
 
     .writeLock ().lock ();
     try
     {
        = sUserDataPath;
     }
     finally
     {
       .writeLock ().unlock ();
     }
   }

  
Get the base path, where all user objects reside. It is relative to the URL context and relative to the servlet context directory.

Returns:
The current user data path. Always starting with a "/", but without any context information. By default the return value is DEFAULT_USER_DATA_PATH.
 
   @Nonnull
   @Nonempty
   public static String getUserDataPath ()
   {
     .readLock ().lock ();
     try
     {
       return ;
     }
    finally
    {
      .readLock ().unlock ();
    }
  }

  
Determine whether file resources are located relative to the servlet context (inside the web application) or inside the data directory (outside the web application). By default the files reside inside the web application.

Parameters:
bServletContextIO true to use servlet context IO, false to use data IO.
  public static void setServletContextIO (final boolean bServletContextIO)
  {
    .writeLock ().lock ();
    try
    {
       = bServletContextIO;
    }
    finally
    {
      .writeLock ().unlock ();
    }
  }
  public static boolean isServletContextIO ()
  {
    .readLock ().lock ();
    try
    {
      return ;
    }
    finally
    {
      .readLock ().unlock ();
    }
  }

  

Parameters:
aRequestScope The request web scope to be used. Required for cookie-less handling. May not be null.
Returns:
Context and user data path. Always starting with a "/". E.g. /user or /context/user
  public static String getContextAndUserDataPath (@Nonnull final IRequestWebScopeWithoutResponse aRequestScope)
  {
    return LinkUtils.getURIWithContext (aRequestScopegetUserDataPath ());
  }

  
Get the URL to the passed UDO object.

Parameters:
aRequestScope The request web scope to be used. Required for cookie-less handling. May not be null.
aUDO The UDO object to get the URL from.
Returns:
The path to the user data object as an URL, including the context path. Always starting with a "/". E.g. /context/user/file.txt if this object points to /file.txt and the user data path is /user .
  public static String getURLPath (@Nonnull final IRequestWebScopeWithoutResponse aRequestScope,
                                   @Nonnull final UserDataObject aUDO)
  {
    ValueEnforcer.notNull (aUDO"UDO");
    return LinkUtils.getURIWithContext (aRequestScopegetUserDataPath () + aUDO.getPath ());
  }

  
Get the URL to the passed UDO object.

Parameters:
aRequestScope The request web scope to be used. Required for cookie-less handling. May not be null.
aUDO The UDO object to get the URL from.
Returns:
The URL to the user data object, including the context path. Always starting with a "/". E.g. /context/user/file.txt if this object points to /file.txt and the user data path is /user .
  public static SimpleURL getURL (@Nonnull final IRequestWebScopeWithoutResponse aRequestScope,
                                  @Nonnull final UserDataObject aUDO)
  {
    return new SimpleURL (getURLPath (aRequestScopeaUDO));
  }
  private static PathRelativeFileIO _getFileIO ()
  {
    return isServletContextIO () ? WebFileIO.getServletContextIO () : WebFileIO.getDataIO ();
  }

  

Returns:
The file system base path for UDOs.
  public static File getBasePathFile ()
  {
    return _getFileIO ().getBasePathFile ();
  }

  
Get the file system resource of the passed UDO object.

Parameters:
aUDO The UDO object to get the resource from.
Returns:
The matching file system resource. No check is performed, whether the resource exists or not!
  public static FileSystemResource getResource (@Nonnull final UserDataObject aUDO)
  {
    if (aUDO == null)
      throw new NullPointerException ("UDO");
    return _getFileIO ().getResource (getUserDataPath () + aUDO.getPath ());
  }

  
Get the File of the passed UDO object.

Parameters:
aUDO The UDO object to get the resource from.
Returns:
The matching File. No check is performed, whether the file exists or not!
  public static File getFile (@Nonnull final UserDataObject aUDO)
  {
    if (aUDO == null)
      throw new NullPointerException ("UDO");
    return _getFileIO ().getFile (getUserDataPath () + aUDO.getPath ());
  }
New to GrepCode? Check out our FAQ X