View Javadoc

1   /*
2    * Copyright 2008 Members of the EGEE Collaboration.
3    * Copyright 2008 University Corporation for Advanced Internet Development, Inc.
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.opensaml.ws.wssecurity.impl;
19  
20  import org.joda.time.DateTime;
21  import org.joda.time.chrono.ISOChronology;
22  import org.joda.time.format.DateTimeFormatter;
23  import org.joda.time.format.ISODateTimeFormat;
24  import org.opensaml.ws.wssecurity.AttributedDateTime;
25  import org.opensaml.ws.wssecurity.IdBearing;
26  import org.opensaml.xml.util.AttributeMap;
27  
28  /**
29   * Implementation of {@link AttributedDateTime}.
30   * 
31   */
32  public class AttributedDateTimeImpl extends AbstractWSSecurityObject implements AttributedDateTime {
33  
34      /** DateTime formatter. */
35      private DateTimeFormatter formatter;
36  
37      /** DateTime object. */
38      private DateTime dateTimeValue;
39  
40      /** String dateTime representation. */
41      private String stringValue;
42      
43      /** wsu:id attribute value. */
44      private String id;
45      
46      /** Wildcard attributes. */
47      private AttributeMap unknownAttributes;
48  
49      /**
50       * Constructor.
51       * 
52       * @param namespaceURI namespace of the element
53       * @param elementLocalName name of the element
54       * @param namespacePrefix namespace prefix of the element
55       */
56      public AttributedDateTimeImpl(String namespaceURI, String elementLocalName, String namespacePrefix) {
57          super(namespaceURI, elementLocalName, namespacePrefix);
58          formatter = ISODateTimeFormat.dateTime().withChronology(ISOChronology.getInstanceUTC());
59          unknownAttributes = new AttributeMap(this);
60      }
61  
62      /** {@inheritDoc} */
63      public DateTime getDateTime() {
64          return dateTimeValue;
65      }
66  
67      /** {@inheritDoc} */
68      public void setDateTime(DateTime newDateTime) {
69          dateTimeValue = newDateTime;
70          String formattedDateTime = formatter.print(dateTimeValue);
71          stringValue = prepareForAssignment(stringValue, formattedDateTime);
72      }
73  
74      /** {@inheritDoc} */
75      public String getValue() {
76          return stringValue;
77      }
78  
79      /** {@inheritDoc} */
80      public void setValue(String newValue) {
81          dateTimeValue = new DateTime(newValue).withChronology(ISOChronology.getInstanceUTC());
82          stringValue = prepareForAssignment(stringValue, newValue);
83      }
84  
85      /** {@inheritDoc} */
86      public String getWSUId() {
87          return id;
88      }
89  
90      /** {@inheritDoc} */
91      public void setWSUId(String newId) {
92          String oldID = id;
93          id = prepareForAssignment(id, newId);
94          registerOwnID(oldID, id);
95          manageQualifiedAttributeNamespace(IdBearing.WSU_ID_ATTR_NAME, id != null);
96      }
97  
98      /** {@inheritDoc} */
99      public AttributeMap getUnknownAttributes() {
100         return unknownAttributes;
101     }
102     
103     /** {@inheritDoc} */
104     public DateTimeFormatter getDateTimeFormatter() {
105         return formatter;
106     }
107 
108     /** {@inheritDoc} */
109     public void setDateTimeFormatter(DateTimeFormatter newFormatter) {
110         if (newFormatter == null) {
111             throw new IllegalArgumentException("The specified DateTimeFormatter may not be null");
112         }
113         formatter = newFormatter;
114         // Explicitly cause the cached string representation to be reformatted when the formatter is changed
115         setDateTime(getDateTime());
116     }
117 
118 }