Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2009 eXo Platform SAS.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
   * the License, or (at your option) any later version.
   *
   * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 package org.exoplatform.services.ftp.command;
 
 
 import java.io.File;
 
 import javax.jcr.Node;
Created by The eXo Platform SAS Author : Vitaly Guly <gavrik-vetal@ukr.net/mail.ru>

Version:
$Id: CmdStor.java 34445 2009-07-24 07:51:18Z dkatayev $
 
 
 public class CmdStor extends FtpCommandImpl
 {
 
    private static final Log LOG = ExoLogger.getLogger(. + "CmdStor");
 
    public CmdStor()
    {
    }
 
    public void run(String[] paramsthrows IOException
    {
       if (clientSession().getDataTransiver() == null)
       {
          reply(..);
          return;
       }
 
       try
       {
          while (!clientSession().getDataTransiver().isConnected())
          {
             Thread.sleep(100);
          }
 
       }
       catch (Exception exc)
       {
          .info("Unhandled exception. " + exc.getMessage(), exc);
       }
 
       if (params.length < 2)
       {
          return;
       }
 
       String fileName = params[1];
 
       try
       {
          ArrayList<StringnewPath = clientSession().getFullPath(fileName);
          
          FtpConfig ftpConfig = clientSession().getFtpServer().getConfiguration();
          if (ftpConfig.isReplaceForbiddenChars())
          {
             String fName = newPath.get(newPath.size()-1);
             String newfName =
                      FtpTextUtils.replaceForbiddenChars(fNameftpConfig.getForbiddenChars(), ftpConfig
                               .getReplaceChar());
             
            fileName  = fileName.substring(0, fileName.indexOf(fName)) + newfName;
         }
         
         Session curSession = clientSession().getSession(newPath.get(0));
         Node resourceNode = getExistedFileNode(curSessionfileName);
         boolean isNeedCheckIn = false;
         if (resourceNode == null)
         {
            if (...equals(clientSession().getPrevCommand()))
            {
               reply(String.format(.."Requested file not exist"));
               return;
            }
            resourceNode = createNewFileNode(curSessionfileName);
         }
         else
         {
            if (...equals(clientSession().getPrevCommand()))
            {
               int restOffset = new Integer(clientSession().getPrevParams());
               if (restOffset > (resourceNode.getProperty(..).getLength() + 1))
               {
                  reply(String.format(.."Restore value invalid"));
                  return;
               }
            }
            if (resourceNode.getParent().isNodeType(..))
            {
               resourceNode.getParent().checkout();
               isNeedCheckIn = true;
            }
         }
         InputStream inputStream = null;
         String cacheFileName = null;
         {
            String cacheFolderName = clientSession().getFtpServer().getConfiguration().getCacheFolderName();
            cacheFileName = cacheFolderName + "/" + IdGenerator.generate() + .;
            File cacheFile = new File(cacheFileName);
            boolean created = cacheFile.createNewFile();
            if (!created)
            {
               reply(String.format(.."STOR"));
               return;
            }
            FileOutputStream cacheOutStream = new FileOutputStream(cacheFile);
            InputStream nodeInputStream = resourceNode.getProperty(..).getStream();
            if (nodeInputStream == null)
            {
               reply(String.format(.."STOR"));
               return;
            }
            byte[] buffer = new byte[32 * 1024];
            while (true)
            {
               int readed = nodeInputStream.read(buffer);
               if (readed < 0)
               {
                  break;
               }
               cacheOutStream.write(buffer, 0, readed);
            }
            cacheOutStream.close();
            RandomAccessFile cacheFilePoint = new RandomAccessFile(cacheFile"rw");
            int restOffset = new Integer(clientSession().getPrevParams());
            cacheFilePoint.seek(restOffset);
            InputStream socketInputStream = clientSession().getDataTransiver().getInputStream();
            reply(..);
            while (true)
            {
               int readed = socketInputStream.read(buffer);
               if (readed < 0)
               {
                  break;
               }
               cacheFilePoint.write(buffer, 0, readed);
            }
            cacheFilePoint.close();
            inputStream = new FileInputStream(cacheFile);
         }
         else
         {
            inputStream = clientSession().getDataTransiver().getInputStream();
            reply(..);
         }
         resourceNode.setProperty(.., Calendar.getInstance());
         resourceNode.setProperty(..inputStream);
         curSession.save();
         clientSession().closeDataTransiver();
         try
         {
            inputStream.close();
         }
         catch (Exception exc)
         {
            .info("Failurinc closing input stream");
         }
         if (cacheFileName != null)
         {
            File cacheFile = new File(cacheFileName);
            cacheFile.delete();
         }
         if (isNeedCheckIn)
         {
            resourceNode.getParent().checkin();
         }
         reply(..);
         return;
      }
      catch (RepositoryException rexc)
      {
         if (.isTraceEnabled())
         {
            .trace("An exception occurred: " + rexc.getMessage());
         }
      }
      catch (Exception exc)
      {
         .info("Unhandled exception. " + exc.getMessage(), exc);
      }
      reply(String.format(..fileName));
   }
   protected Node getExistedFileNode(Session curSessionString resName)
   {
      try
      {
         ArrayList<StringnewPath = clientSession().getFullPath(resName);
         String repoPath = clientSession().getRepoPath(newPath);
         Node fileNode = (Node)curSession.getItem(repoPath);
         return fileNode.getNode(..);
      }
      catch (RepositoryException rexc)
      {
         if (.isTraceEnabled())
         {
            .trace("An exception occurred: " + rexc.getMessage());
         }
      }
      catch (Exception exc)
      {
         .info("Unhandled exception. " + exc.getMessage(), exc);
      }
      return null;
   }
   protected Node createNewFileNode(Session curSessionString resName)
   {
      try
      {
         ArrayList<StringnewPath = clientSession().getFullPath(resName);
         String repoPath = clientSession().getRepoPath(newPath);
         String onlyName = repoPath.substring(repoPath.lastIndexOf("/") + 1);
         String onlyPath = repoPath.substring(0, repoPath.length() - onlyName.length());
         if (onlyPath.length() > 1)
         {
            if (onlyPath.endsWith("/"))
            {
               onlyPath = onlyPath.substring(0, onlyPath.length() - 1);
            }
         }
         Node parentNode = (Node)curSession.getItem(onlyPath);
         FtpConfig configuration = clientSession().getFtpServer().getConfiguration();
         String fileNodeType = configuration.getDefFileNodeType();
         Node fileNode = parentNode.addNode(onlyNamefileNodeType);
         Node dataNode = fileNode.addNode(....);
         MimeTypeResolver mimeTypeResolver = new MimeTypeResolver();
         mimeTypeResolver.setDefaultMimeType(configuration.getDefFileMimeType());
         String mimeType = mimeTypeResolver.getMimeType(onlyName);
         dataNode.setProperty(..mimeType);
         dataNode.setProperty(.., Calendar.getInstance());
         return dataNode;
      }
      catch (PathNotFoundException pexc)
      {
         if (.isTraceEnabled())
         {
            .trace("An exception occurred: " + pexc.getMessage());
         }
      }
      catch (Exception exc)
      {
         .info("Unhandled exception. " + exc.getMessage(), exc);
      }
      return null;
   }
New to GrepCode? Check out our FAQ X