File Source: BookmarksImport.java

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   *  contributor license agreements.  The ASF licenses this file to You
     4   * under the Apache License, Version 2.0 (the "License"); you may not
     5   * use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.  For additional information regarding
    15   * copyright in this work, please see the NOTICE file in the top level
    16   * directory of this distribution.
    17   */
    18  
    19  package org.apache.roller.weblogger.ui.struts2.editor;
    20  
    21  import java.io.ByteArrayOutputStream;
    22  import java.io.File;
    23  import java.io.FileInputStream;
    24  import java.io.InputStream;
    25  import java.text.SimpleDateFormat;
    26  import java.util.Date;
    27  import org.apache.commons.logging.Log;
    28  import org.apache.commons.logging.LogFactory;
    29  import org.apache.roller.weblogger.business.BookmarkManager;
    30  import org.apache.roller.weblogger.business.WebloggerFactory;
    31  import org.apache.roller.weblogger.pojos.WeblogPermission;
    32  import org.apache.roller.weblogger.ui.struts2.util.UIAction;
    33  import org.apache.roller.weblogger.util.cache.CacheManager;
    34  
    35  
    36  /**
    37   * Import opml file into bookmarks folder.
    38   */
    39  public final class BookmarksImport extends UIAction {
    40      
             /* 
    P/P       *  Method: org.apache.roller.weblogger.ui.struts2.editor.BookmarksImport__static_init
              * 
              *  Postconditions:
              *    init'ed(log)
              */
    41      private static Log log = LogFactory.getLog(BookmarksImport.class);
    42      
    43      // uploaded opml file
    44      private File opmlFile = null;
    45      
    46      // content type of uploaded file
    47      private String opmlFileContentType = null;
    48      
    49      // file name of uploaded file
    50      private String opmlFileFileName = null;
    51      
    52      
             /* 
    P/P       *  Method: void org.apache.roller.weblogger.ui.struts2.editor.BookmarksImport()
              * 
              *  Postconditions:
              *    this.actionName == &"bookmarksImport"
              *    this.desiredMenu == &"editor"
              *    this.opmlFile == null
              *    this.opmlFileContentType == null
              *    this.opmlFileFileName == null
              *    this.pageTitle == &"bookmarksImport.title"
              */
    53      public BookmarksImport() {
    54          this.actionName = "bookmarksImport";
    55          this.desiredMenu = "editor";
    56          this.pageTitle = "bookmarksImport.title";
    57      }
    58      
    59      
    60      // author perms required
    61      public short requiredWeblogPermissions() {
                 /* 
    P/P           *  Method: short requiredWeblogPermissions()
                  * 
                  *  Presumptions:
                  *    init'ed(org.apache.roller.weblogger.pojos.WeblogPermission.AUTHOR)
                  * 
                  *  Postconditions:
                  *    return_value == org.apache.roller.weblogger.pojos.WeblogPermission.AUTHOR
                  *    (soft) init'ed(return_value)
                  */
    62          return WeblogPermission.AUTHOR;
    63      }
    64      
    65      
    66      /**
    67       * Request to import bookmarks
    68       */
    69      public String execute() {
                 /* 
    P/P           *  Method: String execute()
                  * 
                  *  Postconditions:
                  *    return_value == &"input"
                  */
    70          return INPUT;
    71      }
    72      
    73      
    74      /**
    75       * Save imported bookmarks.
    76       */
    77      public String save() {
    78          
                 /* 
    P/P           *  Method: String save()
                  * 
                  *  Preconditions:
                  *    init'ed(this.opmlFile)
                  *    (soft) log != null
                  * 
                  *  Presumptions:
                  *    org.apache.roller.weblogger.business.Weblogger:getBookmarkManager(...)@79 != null
                  *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@104 != null
                  *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@79 != null
                  * 
                  *  Postconditions:
                  *    return_value in Addr_Set{&"success",&"input"}
                  * 
                  *  Test Vectors:
                  *    this.opmlFile: Addr_Set{null}, Inverse{null}
                  *    java.io.File:exists(...)@82: {0}, {1}
                  *    java.io.File:length(...)@85: {4_096_000..264-1}, {-263..4_095_999}
                  *    java.io.InputStream:read(...)@92: {-1}, {-231..-2, 0..232-1}
                  */
    79          BookmarkManager bm = WebloggerFactory.getWeblogger().getBookmarkManager();
    80          
    81          InputStream stream = null;
    82          if(getOpmlFile() != null && getOpmlFile().exists()) try {
    83              
    84              //only write files out that are less than 4MB
    85              if (getOpmlFile().length() < (4*1024000)) {
    86                  
    87                  stream = new FileInputStream(getOpmlFile());
    88                  ByteArrayOutputStream baos = new ByteArrayOutputStream();
    89                  
    90                  byte[] buffer = new byte[8192];
    91                  int bytesRead = 0;
    92                  while ((bytesRead=stream.read(buffer,0,8192)) != -1) {
    93                      baos.write(buffer, 0, bytesRead);
    94                  }
    95                  String data = new String(baos.toByteArray());
    96                  
    97                  SimpleDateFormat formatter =
    98                          new SimpleDateFormat("yyyyMMddHHmmss");
    99                  Date now = new Date();
   100                  String folderName = "imported-" + formatter.format(now);
   101                  
   102                  // Use Roller BookmarkManager to import bookmarks
   103                  bm.importBookmarks(getActionWeblog(), folderName, data);
   104                  WebloggerFactory.getWeblogger().flush();
   105                  
   106                  // notify caches
   107                  CacheManager.invalidate(getActionWeblog());
   108                  
   109                  // message to user
   110                  addMessage("bookmarksImport.imported", folderName);
   111                  
   112                  // destroy the temporary file created
   113                  getOpmlFile().delete();
   114                  
   115                  return SUCCESS;
   116                  
   117              } else {
   118                  String data = "The file is greater than 4MB, "
   119                          +" and has not been written to stream."
   120                          +" File Size: "+getOpmlFile().length()+" bytes. "
   121                          +" This is a limitation of this particular "
   122                          +" web application";
   123                  addError("bookmarksImport.error", data);
   124              }
   125              
   126          } catch (Exception ex) {
   127              log.error("ERROR: importing bookmarks", ex);
   128              // TODO: i18n
   129              addError("bookmarksImport.error", ex.toString());
   130          } finally {
   131              if (stream != null) {
   132                  try {
   133                      stream.close();
   134                  } catch (Exception e) {
   135                      log.error("Closing stream",e);
   136                  }
   137              }
   138          }
   139          
   140          return INPUT;
   141      }
   142      
   143      
   144      public File getOpmlFile() {
                 /* 
    P/P           *  Method: File getOpmlFile()
                  * 
                  *  Preconditions:
                  *    init'ed(this.opmlFile)
                  * 
                  *  Postconditions:
                  *    return_value == this.opmlFile
                  *    init'ed(return_value)
                  */
   145          return opmlFile;
   146      }
   147      
   148      public void setOpmlFile(File opmlFile) {
                 /* 
    P/P           *  Method: void setOpmlFile(File)
                  * 
                  *  Postconditions:
                  *    this.opmlFile == opmlFile
                  *    init'ed(this.opmlFile)
                  */
   149          this.opmlFile = opmlFile;
   150      }
   151      
   152      public String getOpmlFileContentType() {
                 /* 
    P/P           *  Method: String getOpmlFileContentType()
                  * 
                  *  Preconditions:
                  *    init'ed(this.opmlFileContentType)
                  * 
                  *  Postconditions:
                  *    return_value == this.opmlFileContentType
                  *    init'ed(return_value)
                  */
   153          return opmlFileContentType;
   154      }
   155      
   156      public void setOpmlFileContentType(String opmlFileContentType) {
                 /* 
    P/P           *  Method: void setOpmlFileContentType(String)
                  * 
                  *  Postconditions:
                  *    this.opmlFileContentType == opmlFileContentType
                  *    init'ed(this.opmlFileContentType)
                  */
   157          this.opmlFileContentType = opmlFileContentType;
   158      }
   159      
   160      public String getOpmlFileFileName() {
                 /* 
    P/P           *  Method: String getOpmlFileFileName()
                  * 
                  *  Preconditions:
                  *    init'ed(this.opmlFileFileName)
                  * 
                  *  Postconditions:
                  *    return_value == this.opmlFileFileName
                  *    init'ed(return_value)
                  */
   161          return opmlFileFileName;
   162      }
   163      
   164      public void setOpmlFileFileName(String opmlFileFileName) {
                 /* 
    P/P           *  Method: void setOpmlFileFileName(String)
                  * 
                  *  Postconditions:
                  *    this.opmlFileFileName == opmlFileFileName
                  *    init'ed(this.opmlFileFileName)
                  */
   165          this.opmlFileFileName = opmlFileFileName;
   166      }
   167      
   168  }








SofCheck Inspector Build Version : 2.18479
BookmarksImport.java 2009-Jan-02 14:24:54
BookmarksImport.class 2009-Sep-04 03:12:45