portals-jetspeed-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From wea...@apache.org
Subject cvs commit: jakarta-jetspeed-2/content-server/src/java/org/apache/jetspeed/contentserver SimpleContentLocator.java ContentLocator.java ContentFilter.java
Date Fri, 12 Mar 2004 20:09:00 GMT
weaver      2004/03/12 12:09:00

  Modified:    content-server/src/java/org/apache/jetspeed/contentserver
                        ContentFilter.java
  Added:       content-server/src/java/org/apache/jetspeed/contentserver
                        SimpleContentLocator.java ContentLocator.java
  Log:
  refactored locator out of the filter
  
  Revision  Changes    Path
  1.5       +113 -118  jakarta-jetspeed-2/content-server/src/java/org/apache/jetspeed/contentserver/ContentFilter.java
  
  Index: ContentFilter.java
  ===================================================================
  RCS file: /home/cvs/jakarta-jetspeed-2/content-server/src/java/org/apache/jetspeed/contentserver/ContentFilter.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- ContentFilter.java	12 Mar 2004 14:31:52 -0000	1.4
  +++ ContentFilter.java	12 Mar 2004 20:09:00 -0000	1.5
  @@ -80,45 +80,40 @@
       public void doFilter(ServletRequest request, ServletResponse response,
               FilterChain chain) throws IOException, ServletException
       {
  -        try
  +        if (request instanceof HttpServletRequest)
           {
  -            if (request instanceof HttpServletRequest)
  +            
  +            HttpServletRequest httpRequest = (HttpServletRequest) request;
  +            HttpServletResponse httpResponse = (HttpServletResponse) response;
  +            String requestURI = httpRequest.getRequestURI();
  +            String mimeType = config.getServletContext()
  +            .getMimeType(requestURI);
  +            
  +            if (mimeType == null) { throw new NullPointerException(
  +                    "MIME-TYPE for "
  +                    + requestURI
  +                    + " could not be located.  Make sure your container is properly configured
to detect MIME types."); }
  +            
  +            System.out.println(mimeType + " detected: " + requestURI);
  +            
  +            SimpleContentLocator contentLocator = new SimpleContentLocator(this.contentDir,
getContentSearchPathes(httpRequest), "/content/", true);
  +            long contentLength = contentLocator.mergeContent(requestURI, response.getOutputStream());
  +            
  +            if (contentLength > -1)
               {
  -
  -                HttpServletRequest httpRequest = (HttpServletRequest) request;
  -                HttpServletResponse httpResponse = (HttpServletResponse) response;
  -                String requestURI = httpRequest.getRequestURI();
  -                String mimeType = config.getServletContext().getMimeType(
  -                        requestURI);
  -
  -                if (mimeType == null) { throw new NullPointerException(
  -                        "MIME-TYPE for "
  -                                + requestURI
  -                                + " could not be located.  Make sure your container is
properly configured to detect MIME types."); }
  -
  -                System.out.println(mimeType + " detected: " + requestURI);
  -
  -                boolean found = setThemeContent(requestURI, httpRequest,
  -                        httpResponse, mimeType);
  -
  -                if (found)
  -                {
  -                    System.out.println("Setting status to OK");
  -                    httpResponse.setStatus(HttpServletResponse.SC_OK);
  -                } else
  -                {
  -                    chain.doFilter(request, response);
  -                }
  -
  -                return;
  -
  +                response.setContentType(mimeType);
  +                response.setContentLength((int) contentLength);
  +                System.out.println("Setting status to OK");
  +                httpResponse.setStatus(HttpServletResponse.SC_OK);
  +            } else
  +            {
  +                chain.doFilter(request, response);
               }
  -        } catch (Exception e)
  -        {
  -
  -            System.out.println("Error filtering image, " + e.toString());
  -            e.printStackTrace();
  +            
  +            return;
  +            
           }
  +        
   
           chain.doFilter(request, response);
       }
  @@ -131,88 +126,88 @@
   
       }
   
  -    protected boolean setThemeContent(String URI, HttpServletRequest request,
  -            HttpServletResponse response, String mimeType)
  -    {
  -        int rootLen = 7;
  -        int rootStart = URI.lastIndexOf("content");
  -        if (rootStart != -1)
  -        {
  -            String dir = URI.substring(rootStart + rootLen);
  -            List pathes = getContentSearchPathes(request);
  -
  -            for (int i = 0; i < pathes.size(); i++)
  -            {
  -                File fqFile = null;
  -                if (fileCache.containsKey(pathes.get(i) + ":" + URI))
  -                {
  -                    fqFile = (File) fileCache.get(pathes.get(i) + ":" + URI);
  -                    System.out.println("Found cached file for URI: "
  -                            + URI);
  -                } 
  -                else
  -                {
  -                    // String fqPath = pathes.get(i) + "/html" + dir;
  -                    String sep="";
  -                    if(pathes.get(i).toString().trim().length() > 1)
  -                    {
  -                        sep = "/";
  -                    }
  -                    String fqPath = contentDir + sep + pathes.get(i) + dir;
  -                    
  -                    fqFile = new File(fqPath);
  -                    System.out.println("Actual content located at: "
  -                            + fqPath);
  -                    System.out.println("Content exists? "
  -                            + fqFile.exists());
  -                    if(!fqFile.exists())
  -                    {
  -                        continue;
  -                    }
  -                    fileCache.put(pathes.get(i) + ":" + URI, fqFile);
  -                }
  -
  -                BufferedInputStream bis = null;
  -                try
  -                {
  -
  -                    bis = new BufferedInputStream(new FileInputStream(fqFile));
  -                    response.setContentType(mimeType);
  -                    response.setContentLength((int) fqFile.length());
  -                    ServletOutputStream sos = response.getOutputStream();
  -                    for (int j = bis.read(); j != -1; j = bis.read())
  -                    {
  -                        sos.write((byte) j);
  -                    }
  -                    System.out.println("Wrote " + fqFile.length()
  -                            + " to the response output stream.");
  -
  -                    return true;
  -
  -                } catch (Exception e)
  -                {
  -                    // TODO Auto-generated catch block
  -                    e.printStackTrace();
  -                } 
  -                finally
  -                {
  -                    try
  -                    {
  -                        if (bis != null)
  -                        {
  -                            bis.close();
  -                        }
  -                    } catch (IOException e1)
  -                    {
  -                        // ignore
  -
  -                    }
  -                }
  -            }
  -        }
  -        return false;
  -
  -    }
  +//    protected boolean setThemeContent(String URI, HttpServletRequest request,
  +//            HttpServletResponse response, String mimeType)
  +//    {
  +//        int rootLen = 7;
  +//        int rootStart = URI.lastIndexOf("content");
  +//        if (rootStart != -1)
  +//        {
  +//            String dir = URI.substring(rootStart + rootLen);
  +//            List pathes = getContentSearchPathes(request);
  +//
  +//            for (int i = 0; i < pathes.size(); i++)
  +//            {
  +//                File fqFile = null;
  +//                if (fileCache.containsKey(pathes.get(i) + ":" + URI))
  +//                {
  +//                    fqFile = (File) fileCache.get(pathes.get(i) + ":" + URI);
  +//                    System.out.println("Found cached file for URI: "
  +//                            + URI);
  +//                } 
  +//                else
  +//                {
  +//                    // String fqPath = pathes.get(i) + "/html" + dir;
  +//                    String sep="";
  +//                    if(pathes.get(i).toString().trim().length() > 1)
  +//                    {
  +//                        sep = "/";
  +//                    }
  +//                    String fqPath = contentDir + sep + pathes.get(i) + dir;
  +//                    
  +//                    fqFile = new File(fqPath);
  +//                    System.out.println("Actual content located at: "
  +//                            + fqPath);
  +//                    System.out.println("Content exists? "
  +//                            + fqFile.exists());
  +//                    if(!fqFile.exists())
  +//                    {
  +//                        continue;
  +//                    }
  +//                    fileCache.put(pathes.get(i) + ":" + URI, fqFile);
  +//                }
  +//
  +//                BufferedInputStream bis = null;
  +//                try
  +//                {
  +//
  +//                    bis = new BufferedInputStream(new FileInputStream(fqFile));
  +//                    response.setContentType(mimeType);
  +//                    response.setContentLength((int) fqFile.length());
  +//                    ServletOutputStream sos = response.getOutputStream();
  +//                    for (int j = bis.read(); j != -1; j = bis.read())
  +//                    {
  +//                        sos.write((byte) j);
  +//                    }
  +//                    System.out.println("Wrote " + fqFile.length()
  +//                            + " to the response output stream.");
  +//
  +//                    return true;
  +//
  +//                } catch (Exception e)
  +//                {
  +//                    // TODO Auto-generated catch block
  +//                    e.printStackTrace();
  +//                } 
  +//                finally
  +//                {
  +//                    try
  +//                    {
  +//                        if (bis != null)
  +//                        {
  +//                            bis.close();
  +//                        }
  +//                    } catch (IOException e1)
  +//                    {
  +//                        // ignore
  +//
  +//                    }
  +//                }
  +//            }
  +//        }
  +//        return false;
  +//
  +//    }
   
       protected List getContentSearchPathes(HttpServletRequest request)
       {
  
  
  
  1.1                  jakarta-jetspeed-2/content-server/src/java/org/apache/jetspeed/contentserver/SimpleContentLocator.java
  
  Index: SimpleContentLocator.java
  ===================================================================
  /*
   * Created on Mar 12, 2004
   *
   * To change the template for this generated file go to
   * Window - Preferences - Java - Code Generation - Code and Comments
   */
  package org.apache.jetspeed.contentserver;
  
  import java.io.BufferedInputStream;
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.IOException;
  import java.io.OutputStream;
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  
  /**
   * <p>
   * SimpleContentLocator
   * </p>
   * 
   * @author <a href="mailto:weaver@apache.org">Scott T. Weaver </a>
   * @version $ $
   *  
   */
  public class SimpleContentLocator implements ContentLocator
  {
  
      private List lookupPathes;
  
      private String rootPath;
  
      private boolean useCachedLookup;
  
      private Map fileCache;
  
      private String URLHint;
  
      public SimpleContentLocator(String rootPath, List lookupPathes,
              String URLHint, boolean useCachedLookup)
      {
          this.lookupPathes = lookupPathes;
          this.rootPath = rootPath;
          this.useCachedLookup = useCachedLookup;
          fileCache = new HashMap();
          this.URLHint = URLHint;
      }
  
      public long mergeContent(String URI, OutputStream os)
      {
          File content = locateContent(URI);
          if(content != null)
          {
              return setContent(content, os);
          }
          else
          {
              return -1;
          }
      }
  
      protected File locateContent(String URI)
      {
          int rootLen = URLHint.length();
          int rootStart = URI.indexOf(URLHint);
          File fqFile = null;
          if (rootStart != -1)
          {
              String dir = URI.substring(rootStart + rootLen);
              
              for (int i = 0; i < lookupPathes.size(); i++)
              {
                  
                  if (useCachedLookup && fileCache.containsKey(lookupPathes.get(i)
+ ":" + URI))
                  {
                      fqFile = (File) fileCache.get(lookupPathes.get(i) + ":"
                              + URI);
                      System.out.println("Found cached file for URI: " + URI);
                  }
                  else
                  {
                      // String fqPath = pathes.get(i) + "/html" + dir;
                      String[] sep = new String[]{"", ""} ;
                      
                      if (lookupPathes.get(i).toString().trim().length() > 1)
                      {
                          sep[0] = "/";
                      }
                      
                      if (!dir.startsWith("/"))
                      {
                          sep[1] = "/";
                      }
                      
                      String fqPath = this.rootPath + sep[0] + lookupPathes.get(i)
                                      + sep[1] + dir;
  
                      fqFile = new File(fqPath);
                      System.out.println("Actual content located at: " + fqPath);
                      System.out.println("Content exists? " + fqFile.exists());
                      if (!fqFile.exists())
                      {
                          fqFile = null;
                          continue;
                      }
                      
                      if(useCachedLookup)
                      {
                          fileCache.put(lookupPathes.get(i) + ":" + URI, fqFile);
                      }
                      return fqFile;
                  }
              }
          }
          
          return null;
          
      }
      
      protected long setContent(File fqFile, OutputStream os)
      {
          BufferedInputStream bis = null;
          try
          {
  
              bis = new BufferedInputStream(new FileInputStream(fqFile));
              for (int j = bis.read(); j != -1; j = bis.read())
              {
                  os.write((byte) j);
              }
              System.out.println("Wrote " + fqFile.length()
                      + " to the output stream.");
  
              return fqFile.length();
  
          } catch (Exception e)
          {
              e.printStackTrace();
              return -1;
          } 
          finally
          {
              try
              {
                  if (bis != null)
                  {
                      bis.close();
                  }
              } catch (IOException e1)
              {
                  // ignore
  
              }
          }
          
      }
  
  }
  
  
  
  1.1                  jakarta-jetspeed-2/content-server/src/java/org/apache/jetspeed/contentserver/ContentLocator.java
  
  Index: ContentLocator.java
  ===================================================================
  /*
   * Created on Mar 12, 2004
   *
   * To change the template for this generated file go to
   * Window - Preferences - Java - Code Generation - Code and Comments
   */
  package org.apache.jetspeed.contentserver;
  
  import java.io.OutputStream;
  
  
  /**
   * <p>
   * ContentLocator
   * </p>
   *
   * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
   * @version $ $
   *
   */
  public interface ContentLocator
  {
      /**
       * 
       * <p>
       *  mergeContent
       * </p>
       * <p>
       *  Merges the content that is located in the provided <code>URI</code>
    * 
       * </p>
       * @param URI Content to locate
       * @param os OutputStream to write the content to.
       * @return int the length of actual content in bytes or -1
       * if the <code>URI</code> was not found.
       */
      long mergeContent(String URI, OutputStream os);
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: jetspeed-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jetspeed-dev-help@jakarta.apache.org


Mime
View raw message