Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */ 
 
 package org.apache.naming.resources;
 
 import java.util.Date;
 
Attributes implementation.

Author(s):
Remy Maucherat
Version:
$Revision: 796 $
 
 public class ResourceAttributes implements Attributes {
     
     
     // -------------------------------------------------------------- Constants
     
     
     // Default attribute names
     
    
Creation date.
 
     public static final String CREATION_DATE = "creationdate";
    
    
    
Creation date.
 
     public static final String ALTERNATE_CREATION_DATE = "creation-date";
    
    
    
Last modification date.
 
     public static final String LAST_MODIFIED = "getlastmodified";
    
    
    
Last modification date.
 
     public static final String ALTERNATE_LAST_MODIFIED = "last-modified";
    
    
    
Name.
 
     public static final String NAME = "displayname";
    
    
    
Type.
 
     public static final String TYPE = "resourcetype";
    
    
    
Type.
 
     public static final String ALTERNATE_TYPE = "content-type";
    
    
    
Source.
 
     public static final String SOURCE = "source";
    
    
    
MIME type of the content.
 
     public static final String CONTENT_TYPE = "getcontenttype";
    
    
    
Content language.
    public static final String CONTENT_LANGUAGE = "getcontentlanguage";
    
    
    
Content length.
    public static final String CONTENT_LENGTH = "getcontentlength";
    
    
    
Content length.
    public static final String ALTERNATE_CONTENT_LENGTH = "content-length";
    
    
    
ETag.
    public static final String ETAG = "getetag";
    
    
    
ETag.
    public static final String ALTERNATE_ETAG = "etag";
    
    
    
Collection type.
    public static final String COLLECTION_TYPE = "<collection/>";
    
    
    
HTTP date format.
    protected static final SimpleDateFormat format =
        new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz".);
    
    
    
Date formats using for Date parsing.
    protected static final SimpleDateFormat formats[] = {
        new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz".),
        new SimpleDateFormat("EEEEEE, dd-MMM-yy HH:mm:ss zzz".),
        new SimpleDateFormat("EEE MMMM d HH:mm:ss yyyy".)
    };
    
    
    protected final static TimeZone gmtZone = TimeZone.getTimeZone("GMT");


    
GMT timezone - all HTTP dates are on GMT
    static {
        .setTimeZone();
        [0].setTimeZone();
        [1].setTimeZone();
        [2].setTimeZone();
    }
    // ----------------------------------------------------------- Constructors
    
    
    
Default constructor.
    public ResourceAttributes() {
    }
    
    
    
Merges with another attribute set.
    public ResourceAttributes(Attributes attributes) {
        this. = attributes;
    }
    
    
    // ----------------------------------------------------- Instance Variables


    
Collection flag.
    protected boolean collection = false;


    
Content length.
    protected long contentLength = -1;


    
Creation time.
    protected long creation = -1;


    
Creation date.
    protected Date creationDate = null;


    
Last modified time.
    protected long lastModified = -1;


    
Last modified date.
    protected Date lastModifiedDate = null;

    
    
Last modified date in HTTP format.
    protected String lastModifiedHttp = null;
    

    
MIME type.
    protected String mimeType = null;
    

    
Name.
    protected String name = null;


    
Weak ETag.
    protected String weakETag = null;


    
Strong ETag.
    protected String strongETag = null;


    
External attributes.
    protected Attributes attributes = null;
    // ------------------------------------------------------------- Properties


    
Is collection.
    public boolean isCollection() {
        if ( != null) {
            return (.equals(getResourceType()));
        } else {
            return ();
        }
    }
    
    
    
Set collection flag.

Parameters:
collection New flag value
    public void setCollection(boolean collection) {
        this. = collection;
        if ( != null) {
            String value = "";
            if (collection)
                value = ;
            .put(value);
        }
    }
    
    
    
Get content length.

Returns:
content length value
    public long getContentLength() {
        if ( != -1L)
            return ;
        if ( != null) {
            Attribute attribute = .get();
            if (attribute != null) {
                try {
                    Object value = attribute.get();
                    if (value instanceof Long) {
                         = ((Longvalue).longValue();
                    } else {
                        try {
                             = Long.parseLong(value.toString());
                        } catch (NumberFormatException e) {
                            ; // Ignore
                        }
                    }
                } catch (NamingException e) {
                    ; // No value for the attribute
                }
            }
        }
        return ;
    }
    
    
    
Set content length.

Parameters:
contentLength New content length value
    public void setContentLength(long contentLength) {
        this. = contentLength;
        if ( != null)
            .put(new Long(contentLength));
    }
    
    
    
Get creation time.

Returns:
creation time value
    public long getCreation() {
        if ( != -1L)
            return ;
        if ( != null)
            return .getTime();
        if ( != null) {
            Attribute attribute = .get();
            if (attribute != null) {
                try {
                    Object value = attribute.get();
                    if (value instanceof Long) {
                         = ((Longvalue).longValue();
                    } else if (value instanceof Date) {
                         = ((Datevalue).getTime();
                         = (Datevalue;
                    } else {
                        String creationDateValue = value.toString();
                        Date result = null;
                        // Parsing the HTTP Date
                        for (int i = 0; (result == null) && 
                                 (i < .); i++) {
                            try {
                                result = [i].parse(creationDateValue);
                            } catch (ParseException e) {
                                ;
                            }
                        }
                        if (result != null) {
                             = result.getTime();
                             = result;
                        }
                    }
                } catch (NamingException e) {
                    ; // No value for the attribute
                }
            }
        }
        return ;
    }
    
    
    
Set creation.

Parameters:
creation New creation value
    public void setCreation(long creation) {
        this. = creation;
        this. = null;
        if ( != null)
            .put(new Date(creation));
    }
    
    
    
Get creation date.

Returns:
Creation date value
    public Date getCreationDate() {
        if ( != null)
            return ;
        if ( != -1L) {
             = new Date();
            return ;
        }
        if ( != null) {
            Attribute attribute = .get();
            if (attribute != null) {
                try {
                    Object value = attribute.get();
                    if (value instanceof Long) {
                         = ((Longvalue).longValue();
                         = new Date();
                    } else if (value instanceof Date) {
                         = ((Datevalue).getTime();
                         = (Datevalue;
                    } else {
                        String creationDateValue = value.toString();
                        Date result = null;
                        // Parsing the HTTP Date
                        for (int i = 0; (result == null) && 
                                 (i < .); i++) {
                            try {
                                result = [i].parse(creationDateValue);
                            } catch (ParseException e) {
                                ;
                            }
                        }
                        if (result != null) {
                             = result.getTime();
                             = result;
                        }
                    }
                } catch (NamingException e) {
                    ; // No value for the attribute
                }
            }
        }
        return ;
    }
    
    
    
Creation date mutator.

Parameters:
creationDate New creation date
    public void setCreationDate(Date creationDate) {
        this. = creationDate.getTime();
        this. = creationDate;
        if ( != null)
            .put(creationDate);
    }
    
    
    
Get last modified time.

Returns:
lastModified time value
    public long getLastModified() {
        if ( != -1L)
            return ;
        if ( != null)
            return .getTime();
        if ( != null) {
            Attribute attribute = .get();
            if (attribute != null) {
                try {
                    Object value = attribute.get();
                    if (value instanceof Long) {
                         = ((Longvalue).longValue();
                    } else if (value instanceof Date) {
                         = ((Datevalue).getTime();
                         = (Datevalue;
                    } else {
                        String lastModifiedDateValue = value.toString();
                        Date result = null;
                        // Parsing the HTTP Date
                        for (int i = 0; (result == null) && 
                                 (i < .); i++) {
                            try {
                                result = 
                                    [i].parse(lastModifiedDateValue);
                            } catch (ParseException e) {
                                ;
                            }
                        }
                        if (result != null) {
                             = result.getTime();
                             = result;
                        }
                    }
                } catch (NamingException e) {
                    ; // No value for the attribute
                }
            }
        }
        return ;
    }
    
    
    
Set last modified.

Parameters:
lastModified New last modified value
    public void setLastModified(long lastModified) {
        this. = lastModified;
        this. = null;
        if ( != null)
            .put(new Date(lastModified));
    }
    
    
    
Set last modified date.

Deprecated:
Parameters:
lastModified New last modified date value
    public void setLastModified(Date lastModified) {
        setLastModifiedDate(lastModified);
    }


    
Get lastModified date.

Returns:
LastModified date value
    public Date getLastModifiedDate() {
        if ( != null)
            return ;
        if ( != -1L) {
             = new Date();
            return ;
        }
        if ( != null) {
            Attribute attribute = .get();
            if (attribute != null) {
                try {
                    Object value = attribute.get();
                    if (value instanceof Long) {
                         = ((Longvalue).longValue();
                         = new Date();
                    } else if (value instanceof Date) {
                         = ((Datevalue).getTime();
                         = (Datevalue;
                    } else {
                        String lastModifiedDateValue = value.toString();
                        Date result = null;
                        // Parsing the HTTP Date
                        for (int i = 0; (result == null) && 
                                 (i < .); i++) {
                            try {
                                result = 
                                    [i].parse(lastModifiedDateValue);
                            } catch (ParseException e) {
                                ;
                            }
                        }
                        if (result != null) {
                             = result.getTime();
                             = result;
                        }
                    }
                } catch (NamingException e) {
                    ; // No value for the attribute
                }
            }
        }
        return ;
    }
    
    
    
Last modified date mutator.

Parameters:
lastModifiedDate New last modified date
    public void setLastModifiedDate(Date lastModifiedDate) {
        this. = lastModifiedDate.getTime();
        this. = lastModifiedDate;
        if ( != null)
            .put(lastModifiedDate);
    }
    
    
    

Returns:
Returns the lastModifiedHttp.
    public String getLastModifiedHttp() {
        if ( != null)
            return ;
        Date modifiedDate = getLastModifiedDate();
        if (modifiedDate == null) {
            modifiedDate = getCreationDate();
        }
        if (modifiedDate == null) {
            modifiedDate = new Date();
        }
        synchronized () {
             = .format(modifiedDate);
        }
        return ;
    }
    
    
    

Parameters:
lastModifiedHttp The lastModifiedHttp to set.
    public void setLastModifiedHttp(String lastModifiedHttp) {
        this. = lastModifiedHttp;
    }
    
    
    

Returns:
Returns the mimeType.
    public String getMimeType() {
        return ;
    }
    
    
    

Parameters:
mimeType The mimeType to set.
    public void setMimeType(String mimeType) {
        this. = mimeType;
    }

    
    
Get name.

Returns:
Name value
    public String getName() {
        if ( != null)
            return ;
        if ( != null) {
            Attribute attribute = .get();
            if (attribute != null) {
                try {
                     = attribute.get().toString();
                } catch (NamingException e) {
                    ; // No value for the attribute
                }
            }
        }
        return ;
    }


    
Set name.

Parameters:
name New name value
    public void setName(String name) {
        this. = name;
        if ( != null)
            .put(name);
    }
    
    
    
Get resource type.

Returns:
String resource type
    public String getResourceType() {
        String result = null;
        if ( != null) {
            Attribute attribute = .get();
            if (attribute != null) {
                try {
                    result = attribute.get().toString();
                } catch (NamingException e) {
                    ; // No value for the attribute
                }
            }
        }
        if (result == null) {
            if ()
                result = ;
            else
                result = null;
        }
        return result;
    }
    
    
    
Type mutator.

Parameters:
resourceType New resource type
    public void setResourceType(String resourceType) {
         = resourceType.equals();
        if ( != null)
            .put(resourceType);
    }


    
Get ETag.

Returns:
strong ETag if available, else weak ETag.
    public String getETag() {
        String result = null;
        if ( != null) {
            Attribute attribute = .get();
            if (attribute != null) {
                try {
                    result = attribute.get().toString();
                } catch (NamingException e) {
                    ; // No value for the attribute
                }
            }
        }
        if (result == null) {
            if ( != null) {
                // The strong ETag must always be calculated by the resources
                result = ;
            } else {
                // The weakETag is contentLength + lastModified
                if ( == null) {
                    long contentLength = getContentLength();
                    long lastModified = getLastModified();
                    if ((contentLength >= 0) || (lastModified >= 0)) {
                         = "W/\"" + contentLength + "-" 
                        + lastModified + "\"";
                    }
                }
                result = ;
            }
        }
        return result;
    }


    
Set strong ETag.
    public void setETag(String eTag) {
        this. = eTag;
        if ( != null)
            .put(eTag);
    }

    
    
Return the canonical path of the resource, to possibly be used for direct file serving. Implementations which support this should override it to return the file path.

Returns:
The canonical path of the resource
    public String getCanonicalPath() {
        return null;
    }
    
    
    // ----------------------------------------------------- Attributes Methods


    
Get attribute.
    public Attribute get(String attrID) {
        if ( == null) {
            if (attrID.equals()) {
                Date creationDate = getCreationDate();
                if (creationDate == nullreturn null;
                return new BasicAttribute(creationDate);
            } else if (attrID.equals()) {
                Date creationDate = getCreationDate();
                if (creationDate == nullreturn null;
                return new BasicAttribute(creationDate);
            } else if (attrID.equals()) {
                Date lastModifiedDate = getLastModifiedDate();
                if (lastModifiedDate == nullreturn null;
                return new BasicAttribute(lastModifiedDate);
            } else if (attrID.equals()) {
                Date lastModifiedDate = getLastModifiedDate();
                if (lastModifiedDate == nullreturn null;
                return new BasicAttribute(lastModifiedDate);
            } else if (attrID.equals()) {
                String name = getName();
                if (name == nullreturn null;
                return new BasicAttribute(name);
            } else if (attrID.equals()) {
                String resourceType = getResourceType();
                if (resourceType == nullreturn null;
                return new BasicAttribute(resourceType);
            } else if (attrID.equals()) {
                String resourceType = getResourceType();
                if (resourceType == nullreturn null;
                return new BasicAttribute(resourceType);
            } else if (attrID.equals()) {
                long contentLength = getContentLength();
                if (contentLength < 0) return null;
                return new BasicAttribute(new Long(contentLength));
            } else if (attrID.equals()) {
              long contentLength = getContentLength();
              if (contentLength < 0) return null;
              return new BasicAttribute(new Long(contentLength));
            } else if (attrID.equals()) {
              String etag = getETag();
              if (etag == nullreturn null;
              return new BasicAttribute(etag);
            } else if (attrID.equals()) {
              String etag = getETag();
              if (etag == nullreturn null;
              return new BasicAttribute(etag);
            }
        } else {
            return .get(attrID);
        }
        return null;
    }
    
    
    
Put attribute.
    public Attribute put(Attribute attribute) {
        if ( == null) {
            try {
                return put(attribute.getID(), attribute.get());
            } catch (NamingException e) {
                return null;
            }
        } else {
            return .put(attribute);
        }
    }
    
    
    
Put attribute.
    public Attribute put(String attrIDObject val) {
        if ( == null) {
            return null// No reason to implement this
        } else {
            return .put(attrIDval);
        }
    }
    
    
    
Remove attribute.
    public Attribute remove(String attrID) {
        if ( == null) {
            return null// No reason to implement this
        } else {
            return .remove(attrID);
        }
    }
    
    
    
Get all attributes.
    public NamingEnumeration getAll() {
        if ( == null) {
            Vector attributes = new Vector();
            Date creationDate = getCreationDate();
            if (creationDate != null) {
                attributes.addElement(new BasicAttribute
                                      (creationDate));
                attributes.addElement(new BasicAttribute
                                      (creationDate));
            }
            Date lastModifiedDate = getLastModifiedDate();
            if (lastModifiedDate != null) {
                attributes.addElement(new BasicAttribute
                                      (lastModifiedDate));
                attributes.addElement(new BasicAttribute
                                      (lastModifiedDate));
            }
            String name = getName();
            if (name != null) {
                attributes.addElement(new BasicAttribute(name));
            }
            String resourceType = getResourceType();
            if (resourceType != null) {
                attributes.addElement(new BasicAttribute(resourceType));
                attributes.addElement(new BasicAttribute(resourceType));
            }
            long contentLength = getContentLength();
            if (contentLength >= 0) {
                Long contentLengthLong = new Long(contentLength);
                attributes.addElement(new BasicAttribute(contentLengthLong));
                attributes.addElement(new BasicAttribute(contentLengthLong));
            }
            String etag = getETag();
            if (etag != null) {
                attributes.addElement(new BasicAttribute(etag));
                attributes.addElement(new BasicAttribute(etag));
            }
            return new RecyclableNamingEnumeration(attributes);
        } else {
            return .getAll();
        }
    }
    
    
    
Get all attribute IDs.
    public NamingEnumeration getIDs() {
        if ( == null) {
            Vector attributeIDs = new Vector();
            Date creationDate = getCreationDate();
            if (creationDate != null) {
                attributeIDs.addElement();
                attributeIDs.addElement();
            }
            Date lastModifiedDate = getLastModifiedDate();
            if (lastModifiedDate != null) {
                attributeIDs.addElement();
                attributeIDs.addElement();
            }
            if (getName() != null) {
                attributeIDs.addElement();
            }
            String resourceType = getResourceType();
            if (resourceType != null) {
                attributeIDs.addElement();
                attributeIDs.addElement();
            }
            long contentLength = getContentLength();
            if (contentLength >= 0) {
                attributeIDs.addElement();
                attributeIDs.addElement();
            }
            String etag = getETag();
            if (etag != null) {
                attributeIDs.addElement();
                attributeIDs.addElement();
            }
            return new RecyclableNamingEnumeration(attributeIDs);
        } else {
            return .getIDs();
        }
    }
    
    
    
Retrieves the number of attributes in the attribute set.
    public int size() {
        if ( == null) {
            int size = 0;
            if (getCreationDate() != nullsize += 2;
            if (getLastModifiedDate() != nullsize += 2;
            if (getName() != nullsize++;
            if (getResourceType() != nullsize += 2;
            if (getContentLength() >= 0) size += 2;
            if (getETag() != nullsize += 2;
            return size;
        } else {
            return .size();
        }
    }
    
    
    
Clone the attributes object (WARNING: fake cloning).
    public Object clone() {
        return this;
    }
    
    
    
Case sensitivity.
    public boolean isCaseIgnored() {
        return false;
    }
    
    
New to GrepCode? Check out our FAQ X