001: /*
002: * $Id: TimestampPolicy.java,v 1.6 2007/03/01 06:34:59 ashutoshshahi Exp $
003: */
004:
005: /*
006: * The contents of this file are subject to the terms
007: * of the Common Development and Distribution License
008: * (the License). You may not use this file except in
009: * compliance with the License.
010: *
011: * You can obtain a copy of the license at
012: * https://glassfish.dev.java.net/public/CDDLv1.0.html.
013: * See the License for the specific language governing
014: * permissions and limitations under the License.
015: *
016: * When distributing Covered Code, include this CDDL
017: * Header Notice in each file and include the License file
018: * at https://glassfish.dev.java.net/public/CDDLv1.0.html.
019: * If applicable, add the following below the CDDL Header,
020: * with the fields enclosed by brackets [] replaced by
021: * you own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
025: */
026:
027: package com.sun.xml.wss.impl.policy.mls;
028:
029: import com.sun.xml.wss.impl.MessageConstants;
030: import java.util.Date;
031: import java.text.SimpleDateFormat;
032:
033: import com.sun.xml.wss.impl.PolicyTypeUtil;
034:
035: /**
036: * A policy representing a WSS Timestamp element.
037: * Note: The TimestampPolicy is the only WSSPolicy element that does not contain a
038: * concrete FeatureBinding and/or KeyBinding.
039: */
040: public class TimestampPolicy extends WSSPolicy {
041:
042: /*
043: * Feature Bindings
044: * Key Bindings
045: */
046:
047: private String creationTime = MessageConstants._EMPTY;
048: private String expirationTime = MessageConstants._EMPTY;
049:
050: private long timeout = 300000;
051: private long maxClockSkew = 300000;
052: private long timestampFreshness = 300000;
053:
054: static SimpleDateFormat formatter = new SimpleDateFormat(
055: "yyyy-MM-dd'T'HH:mm:ss'Z'");
056: static SimpleDateFormat formatter1 = new SimpleDateFormat(
057: "yyyy-MM-dd'T'HH:mm:ss'.'SSS'Z'");
058:
059: /**
060: *Default constructor
061: */
062: public TimestampPolicy() {
063: setPolicyIdentifier(PolicyTypeUtil.TIMESTAMP_POLICY_TYPE);
064: }
065:
066: /**
067: * set the CreationTime for the timestamp in this TimestampPolicy
068: * @param creationTime
069: */
070: public void setCreationTime(String creationTime) {
071: this .creationTime = creationTime;
072: }
073:
074: /**
075: * If the current time on a receiving system is past the CreationTime of the timestamp plus the
076: * timeout, then the timestamp is to be considered expired.
077: * @param timeout the number of milliseconds after which the Timestamp in this
078: * TimestampPolicy will expire.
079: */
080: public void setTimeout(long timeout) {
081: this .timeout = timeout;
082: }
083:
084: /**
085: * set the maximum clock skew adjustment value (in milliseconds)
086: * @param maxClockSkew the Maximum Clock Skew adjustment to be used
087: * when validating received timestamps
088: */
089: public void setMaxClockSkew(long maxClockSkew) {
090: this .maxClockSkew = maxClockSkew;
091: }
092:
093: /**
094: * set the Timestamp Freshness Limit (in milliseconds) for this Timestamp
095: * Timestamps received by a receiver with creation Times older than
096: * the Timestamp Freshness Limit period are supposed to be rejected by the receiver.
097: * @param timestampFreshness the Timestamp Freshness Limit (in milliseconds)
098: */
099: public void setTimestampFreshness(long timestampFreshness) {
100: this .timestampFreshness = timestampFreshness;
101: }
102:
103: /**
104: * @return creationTime the creation time of the timestamp in this
105: * TimestampPolicy if set, empty string otherwise
106: */
107: public String getCreationTime() {
108: return this .creationTime;
109: }
110:
111: /**
112: * @return timeout the Timeout in milliseconds for this Timestamp
113: */
114: public long getTimeout() {
115: return this .timeout;
116: }
117:
118: /**
119: * @return expirationTime the expiration time if set for this Timestamp, empty string otherwise
120: */
121: public String getExpirationTime() throws Exception {
122: if (expirationTime.equals("") && timeout != 0
123: && !creationTime.equals("")) {
124: try {
125: synchronized (formatter) {
126: expirationTime = Long.toString(((Date) formatter
127: .parse(creationTime)).getTime()
128: + timeout);
129: }
130: } catch (Exception e) {
131: synchronized (formatter1) {
132: expirationTime = Long.toString(((Date) formatter1
133: .parse(creationTime)).getTime()
134: + timeout);
135:
136: }
137: }
138: }
139:
140: return this .expirationTime;
141: }
142:
143: /**
144: * @param expirationTime the expiration time
145: */
146: public void setExpirationTime(String expirationTime) {
147: this .expirationTime = expirationTime;
148: }
149:
150: /**
151: * @return maxClockSkew the maximum Clock Skew adjustment
152: */
153: public long getMaxClockSkew() {
154: return this .maxClockSkew;
155: }
156:
157: /**
158: * @return timeStampFreshness limit
159: */
160: public long getTimestampFreshness() {
161: return this .timestampFreshness;
162: }
163:
164: /**
165: * @param policy the policy to be compared for equality
166: * @return true if the argument policy is equal to this
167: */
168: public boolean equals(WSSPolicy policy) {
169:
170: boolean assrt = false;
171:
172: try {
173: TimestampPolicy tPolicy = (TimestampPolicy) policy;
174: boolean b1 = creationTime.equals("") ? true : creationTime
175: .equalsIgnoreCase(tPolicy.getCreationTime());
176: boolean b2 = getExpirationTime().equals("") ? true
177: : getExpirationTime().equalsIgnoreCase(
178: tPolicy.getExpirationTime());
179: assrt = b1 && b2;
180: } catch (Exception e) {
181: }
182:
183: return assrt;
184: }
185:
186: /*
187: * Equality comparision ignoring the Targets
188: * @param policy the policy to be compared for equality
189: * @return true if the argument policy is equal to this
190: */
191: public boolean equalsIgnoreTargets(WSSPolicy policy) {
192: return equals(policy);
193: }
194:
195: /**
196: * Clone operator
197: * @return clone of this policy
198: */
199: public Object clone() {
200: TimestampPolicy tPolicy = new TimestampPolicy();
201:
202: try {
203: tPolicy.setTimeout(timeout);
204: tPolicy.setCreationTime(creationTime);
205: tPolicy.setExpirationTime(expirationTime);
206: tPolicy.setMaxClockSkew(maxClockSkew);
207: tPolicy.setTimestampFreshness(timestampFreshness);
208: } catch (Exception e) {
209: }
210:
211: return tPolicy;
212: }
213:
214: /**
215: * @return the type of the policy
216: */
217: public String getType() {
218: return PolicyTypeUtil.TIMESTAMP_POLICY_TYPE;
219: }
220:
221: }
|