Skip to main content

Restricting attachment size

Rational staff, Staff, IBM
This article is brought to you by the Rational staff at developerWorks.

Summary:  This is an example of a policy enforcement hook for ClearQuest.

Date:  18 May 2004
Level:  Introductory
Activity:  512 views
Comments:  

This item was originally published in May, 2002.

NOTE: This hook is presented only as an example of how to customize your use of IBM® Rational® ClearQuest®. It has not been formally tested, and is not supported by IBM.

Description

IBM® Rational® ClearQuest® Web supports uploading and viewing attachments just like native clients do, but is built in such a way that all other concurrent ClearQuest Web users on the same Web server will be frozen until the file upload/download is complete. One solution is to limit the size of the attachments before they are ever stored in the database. To accomplish this, we will depend on knowing a few facts about the inner workings of ClearQuest.

  • Attachments can be added whether the record is in an editable state or not, and through the API, but in all cases adding an attachment performs an action on the record.
  • The file upload is not done until that action is committed.
  • Between the time that the file was attached and the action is committed, that attachment's size when accessed via the CQ API returns 0 (this is how we can tell which attachments have just been added).

By creating a Base Action, we can create a single Validation hook that executes during the context of all other actions. We will inspect the file size of the attachment(s), and prohibit the action from being committed if any is over a certain limit.

First we iterate through all attachments, checking their size with the attachment.FileSize method. If it returns 0, we know that the file has been added during the current action, and is still on the local drive, so we check the FileSystemObject's filesize. If this size is larger than the limit, we return an error string from the validation hook, that aborts the commit. Returning an empty string allows the commit to proceed.


Script Language

VB Script


Code

Base Action Validation hook:

Function Defect_Validation(actionname, actiontype)
  ' actionname As String
  ' actiontype As Long
  ' Defect_Validation As String
  ' action is AttachAuthorization
  ' record type name is ChangeRequest
   REM Return a non-empty string explaining why the action
   REM cannot commit with the current values.
   REM Or, if it is valid, return an empty string value.
   REM Example: 
   REM    Dim value_info
   REM    Set value_info = GetFieldValue("some field")
   REM    If Len(value_info.GetValue())   10 Then
   REM        Defect_Validation = "Must be at least 10 chars long"
   REM    End If

  Dim fso, f, s
     Defect_Validation = ""
     set session = GetSession()
     set attachFields = AttachmentFields

     ' Iterate over the attachment fields on an Entity.
     For Each attachField In attachFields
        set attaches = attachField.Attachments
        ' iterate over the attachment's field attachments
        For Each myAttach In attaches
              filename = myAttach.FileName
              filesize = myAttach.FileSize
              ' INFO "filenmae is " & filename
              ' File size is always 0 before a first commit
              If (filesize = 0) Then
                  Set fso = CreateObject("Scripting.FileSystemObject")
                  Set f = fso.GetFile(filename)
                  filesize = f.size
        End if
         ' INFO "filename is " & filename & " size is " & filesize
          If (filesize   1500000) Then
           Defect_Validation = "File " & filename & " is too big "
               Exit Function
            End If
        Next
     Next
End Function

Hook Submitter:
Francois Salazar
francois.salazar@comtech-software.fr


About the author

This article is brought to you by the Rational staff at developerWorks.

Comments



Trademarks  |  My developerWorks terms and conditions

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Rational
ArticleID=3883
ArticleTitle=Restricting attachment size
publish-date=05182004
author1-email=
author1-email-cc=

My developerWorks community

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere).

My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Rate a product. Write a review.

Special offers