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.jcr.webdav.lnkproducer;
 
Created by The eXo Platform SAS Author : Vitaly Guly.

Version:
$Id: $
 
 
 public class LinkGenerator
 {

   
MICROSOFT *.LNK FILE HEADER.
 
    public static int[] linkHeader = {
    // 0h 1 dword Always 0000004Ch 'L'
       0x4C, 0x00, 0x00, 0x00,
 
       // 4h 16 bytes GUID of shortcut files
       0x01, 0x14/* 0x04 */, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46,
 
       // 14h 1 dword Flags
       0x81/* 0x00 */, 0x00, 0x00, 0x00,
 
       // 18h 1 dword File attributes
       0x00, 0x00, 0x00, 0x00,
 
       // 1Ch 1 qword Time 1
       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 
       // 24h 1 qword Time 2
       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 
       // 2Ch 1 qword Time 3
       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 
       // 34h 1 dword File length
       0x00, 0x00, 0x00, 0x00,
 
       // 38h 1 dword Icon number
       0x00, 0x00, 0x00, 0x00,
 
       // 3Ch 1 dword ShowWnd value
       0x01, 0x00, 0x00, 0x00,
 
       // 40h 1 dword Hot key
       0x00, 0x00, 0x00, 0x00,
 
       // 44h 2 dwords Unknown, always zero
       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

   
Host name.
 
    private String hostName;

   
Servlet path.
 
    private String servletPath;

   
Traget path.
 
    private String targetPath;

   
Constructor.

Parameters:
hostName host name
servletPath servlet path
targetPath target path
 
    public LinkGenerator(String hostNameString servletPathString targetPath)
    {
       this. = hostName;
      this. = servletPath;
      this. = targetPath;
   }

   
Generates the content of link.

Returns:
link content
Throws:
java.io.IOException java.io.IOException
   public byte[] generateLinkContent() throws IOException
   {
      ByteArrayOutputStream outStream = new ByteArrayOutputStream();
      // LINK HEADER
      for (int i = 0; i < .i++)
      {
         byte curByteValue = (byte)[i];
         outStream.write(curByteValue);
      }
      // LINK BODY
      byte[] linkContent = getLinkContent();
      writeInt(linkContent.length + 2, outStream);
      outStream.write(linkContent);
      // WRITE END LINK FILE
      for (int i = 0; i < 6; i++)
      {
         outStream.write(0);
      }
      return outStream.toByteArray();
   }

   
Gets the content of the link.

Returns:
link content
Throws:
java.io.IOException java.io.IOException
   private byte[] getLinkContent() throws IOException
   {
      ByteArrayOutputStream outStream = new ByteArrayOutputStream();
      byte[] firstItem = getFirstItem();
      writeInt(firstItem.length + 2, outStream);
      writeBytes(firstItemoutStream);
      byte[] lastItem = getLastItem();
      writeInt(lastItem.length + 2, outStream);
      writeBytes(lastItemoutStream);
      String[] pathes = .split("/");
      String root = pathes[pathes.length - 1];
      byte[] rootItem = getRootItem(root);
      writeInt(rootItem.length + 2, outStream);
      writeBytes(rootItemoutStream);
      pathes = .split("/");
      StringBuilder curHref = new StringBuilder();
      for (int i = 0; i < pathes.lengthi++)
      {
         if ("".equals(pathes[i]))
         {
            continue;
         }
         String curName = pathes[i];
         curHref.append("/").append(curName);
         if (i < pathes.length - 1)
         {
            byte[] linkItem = getHreffedFolder(curNamecurHref.toString());
            writeInt(linkItem.length + 2, outStream);
            writeBytes(linkItemoutStream);
         }
         else
         {
            byte[] linkFile = getHreffedFile(curNamecurHref.toString());
            writeInt(linkFile.length + 2, outStream);
            writeBytes(linkFileoutStream);
         }
      }
      return outStream.toByteArray();
   }

   
Returns the first item.

Returns:
first item
Throws:
java.io.IOException java.io.IOException
   private byte[] getFirstItem() throws IOException
   {
      ByteArrayOutputStream outStream = new ByteArrayOutputStream();
      int[] firstItem =
         {0x1F, 0x50, 0xE0, 0x4F, 0xD0, 0x20, 0xEA, 0x3A, 0x69, 0x10, 0xA2, 0xD8, 0x08, 0x00, 0x2B, 0x30, 0x30, 0x9D,};
      writeInts(firstItemoutStream);
      return outStream.toByteArray();
   }

   
Returns the last item.

Returns:
last item
Throws:
java.io.IOException java.io.IOException
   private byte[] getLastItem() throws IOException
   {
      ByteArrayOutputStream outStream = new ByteArrayOutputStream();
      int[] lastItem =
         {0x2E, 0x80, 0x00, 0xDF, 0xEA, 0xBD, 0x65, 0xC2, 0xD0, 0x11, 0xBC, 0xED, 0x00, 0xA0, 0xC9, 0x0A, 0xB5, 0x0F};
      writeInts(lastItemoutStream);
      return outStream.toByteArray();
   }

   
Returns the root item.

Parameters:
rootName root item name.
servlet servlet name.
Returns:
root item
Throws:
java.io.IOException java.io.IOException
   private byte[] getRootItem(String rootNameString servletthrows IOException
   {
      ByteArrayOutputStream outStream = new ByteArrayOutputStream();
      byte[] rootHead = getRootHeader();
      writeBytes(rootHeadoutStream);
      byte[] rootValue = getRootValue(rootName);
      writeInt(rootValue.length / 2, outStream);
      writeBytes(rootValueoutStream);
      outStream.write(0);
      outStream.write(0);
      writeSizedString(servletoutStream);
      writeZeroQWord(outStream);
      return outStream.toByteArray();
   }

   
Returns the root item.

Returns:
root item
Throws:
java.io.IOException java.io.IOException
   private byte[] getRootHeader() throws IOException
   {
      ByteArrayOutputStream outStream = new ByteArrayOutputStream();
      int[] rootHeader =
         {0x4C, 0x50, 0x00, 0x01, 0x42, 0x57, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00};
      writeInts(rootHeaderoutStream);
      return outStream.toByteArray();
   }

   
Returns the root value.

Parameters:
rootName root item name.
Returns:
root item value
Throws:
java.io.IOException java.io.IOException
   private byte[] getRootValue(String rootNamethrows IOException
   {
      ByteArrayOutputStream outStream = new ByteArrayOutputStream();
      simpleWriteString(rootNameoutStream);
      int[] rootVal = {0x20, 0x00, 0x3D, 0x04, 0x30, 0x04, 0x20, 0x00};
      writeInts(rootValoutStream);
      simpleWriteString(outStream);
      return outStream.toByteArray();
   }
   private byte[] getHreffedFolder(String itemNameString itemHrefthrows IOException
   {
      ByteArrayOutputStream outStream = new ByteArrayOutputStream();
      int[] linkHrefHeader =
         {0x4C, 0x50, 0x00, 0x22, 0x42, 0x57, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x40};
      writeInts(linkHrefHeaderoutStream);
      writeSizedString(itemNameoutStream);
      writeSizedString(itemHrefoutStream);
      writeZeroQWord(outStream);
      return outStream.toByteArray();
   }
   private byte[] getHreffedFile(String itemNameString itemHrefthrows IOException
   {
      ByteArrayOutputStream outStream = new ByteArrayOutputStream();
      int[] attrs =
         {0x4C, 0x50, 0x00, 0x22, 0x42, 0x57, 0xF0, 0x43, 0x1C, 0x29, 0xB6, 0x5C, 0xC7, 0x01, 0x00, 0x7C, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00};
      writeInts(attrsoutStream);
      writeSizedString(itemNameoutStream);
      writeSizedString(itemHrefoutStream);
      writeZeroQWord(outStream);
      return outStream.toByteArray();
   }

   
Writes string into stream.

Parameters:
outString string
outStream stream
Throws:
java.io.IOException java.io.IOException
   private void simpleWriteString(String outStringOutputStream outStreamthrows IOException
   {
      for (int i = 0; i < outString.length(); i++)
      {
         int charCode = outString.charAt(i);
         outStream.write(charCode & 0xFF);
         outStream.write((charCode >> 8) & 0xFF);
      }
   }

   
Writes zero-string into stream.

Parameters:
outString string
outStream stream
Throws:
java.io.IOException java.io.IOException
   private void writeZeroString(String outStringOutputStream outStreamthrows IOException
   {
      simpleWriteString(outStringoutStream);
      outStream.write(0);
      outStream.write(0);
   }
   private void writeSizedString(String outStringOutputStream outStreamthrows IOException
   {
      int stringLength = outString.length();
      writeInt(stringLengthoutStream);
      writeZeroString(outStringoutStream);
   }

   
Writes int into stream.

Parameters:
intValue int value
outStream stream
Throws:
java.io.IOException java.io.IOException
   private void writeInt(int intValueOutputStream outStreamthrows IOException
   {
      outStream.write(intValue & 0xFF);
      outStream.write((intValue >> 8) & 0xFF);
   }

   
Writes int array into stream.

Parameters:
bytes int array
outStream stream
Throws:
java.io.IOException java.io.IOException
   private void writeInts(int[] bytesOutputStream outStreamthrows IOException
   {
      for (int i = 0; i < bytes.lengthi++)
      {
         byte curByte = (byte)bytes[i];
         outStream.write(curByte);
      }
   }

   
Writes byte array into stream.

Parameters:
bytes byte array
outStream stream
Throws:
java.io.IOException java.io.IOException
   private void writeBytes(byte[] bytesOutputStream outStreamthrows IOException
   {
      for (int i = 0; i < bytes.lengthi++)
      {
         byte curByte = bytes[i];
         outStream.write(curByte);
      }
   }
   private void writeZeroQWord(OutputStream outStreamthrows IOException
   {
      int[] zeroQWord = {0x00, 0x00, 0x00, 0x00};
      writeInts(zeroQWordoutStream);
   }
New to GrepCode? Check out our FAQ X