001: /*
002: * Timestamp.java
003: *
004: * Created on August 30, 2006, 11:16 PM
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.ws.security.opt.impl.tokens;
028:
029: import com.sun.xml.ws.security.opt.api.SecurityElementWriter;
030: import com.sun.xml.ws.security.opt.api.SecurityHeaderElement;
031: import com.sun.xml.ws.security.opt.impl.util.JAXBUtil;
032: import com.sun.xml.ws.security.wsu10.AttributedDateTime;
033: import com.sun.xml.ws.security.wsu10.ObjectFactory;
034: import com.sun.xml.ws.security.wsu10.TimestampType;
035: import java.io.OutputStream;
036: import java.text.SimpleDateFormat;
037: import java.util.Calendar;
038: import java.util.GregorianCalendar;
039: import java.util.HashMap;
040: import java.util.Iterator;
041: import java.util.Map;
042: import javax.xml.bind.JAXBElement;
043: import javax.xml.bind.Marshaller;
044: import javax.xml.bind.JAXBException;
045: import javax.xml.namespace.QName;
046: import com.sun.xml.wss.impl.MessageConstants;
047: import com.sun.xml.stream.buffer.XMLStreamBufferResult;
048: import com.sun.xml.ws.api.SOAPVersion;
049: import javax.xml.stream.XMLStreamException;
050:
051: import com.sun.xml.wss.XWSSecurityException;
052:
053: /**
054: * Representation of Timestamp SecurityHeaderElement
055: * @author Ashutosh.Shahi@sun.com
056: */
057: public class Timestamp extends TimestampType implements
058: com.sun.xml.ws.security.opt.api.tokens.Timestamp,
059: SecurityHeaderElement, SecurityElementWriter {
060:
061: private long timeout = 0;
062: private SOAPVersion soapVersion = SOAPVersion.SOAP_11;
063: private ObjectFactory objFac = new ObjectFactory();
064:
065: /**
066: * Creates a new instance of Timestamp
067: * @param sv the soapVersion for this message
068: */
069: public Timestamp(SOAPVersion sv) {
070: this .soapVersion = sv;
071: }
072:
073: /**
074: *
075: * @param created set the creation time on timestamp
076: */
077: public void setCreated(final String created) {
078: AttributedDateTime timeCreated = objFac
079: .createAttributedDateTime();
080: timeCreated.setValue(created);
081: setCreated(timeCreated);
082: }
083:
084: /**
085: *
086: * @param expires set the expiry time on timestamp
087: */
088: public void setExpires(final String expires) {
089: AttributedDateTime timeExpires = objFac
090: .createAttributedDateTime();
091: timeExpires.setValue(expires);
092: setExpires(timeExpires);
093: }
094:
095: /**
096: *
097: * @return the creation time value
098: */
099: public String getCreatedValue() {
100: String createdValue = null;
101: AttributedDateTime created = getCreated();
102: if (created != null)
103: createdValue = created.getValue();
104: return createdValue;
105: }
106:
107: /**
108: *
109: * @return the expiry time value
110: */
111: public String getExpiresValue() {
112: String expiresValue = null;
113: AttributedDateTime expires = getExpires();
114: if (expires != null)
115: expiresValue = expires.getValue();
116: return expiresValue;
117: }
118:
119: /**
120: * The timeout is assumed to be in seconds
121: */
122: public void setTimeout(long timeout) {
123: this .timeout = timeout;
124: }
125:
126: public String getNamespaceURI() {
127: return MessageConstants.WSU_NS;
128: }
129:
130: public String getLocalPart() {
131: return MessageConstants.TIMESTAMP_LNAME;
132: }
133:
134: public String getAttribute(String nsUri, String localName) {
135: QName qname = new QName(nsUri, localName);
136: Map<QName, String> otherAttributes = this .getOtherAttributes();
137: return otherAttributes.get(qname);
138: }
139:
140: public String getAttribute(QName name) {
141: Map<QName, String> otherAttributes = this .getOtherAttributes();
142: return otherAttributes.get(name);
143: }
144:
145: public javax.xml.stream.XMLStreamReader readHeader()
146: throws javax.xml.stream.XMLStreamException {
147: XMLStreamBufferResult xbr = new XMLStreamBufferResult();
148: JAXBElement<TimestampType> tsElem = new ObjectFactory()
149: .createTimestamp(this );
150: try {
151: getMarshaller().marshal(tsElem, xbr);
152:
153: } catch (JAXBException je) {
154: throw new XMLStreamException(je);
155: }
156: return xbr.getXMLStreamBuffer().readAsXMLStreamReader();
157: }
158:
159: /**
160: *
161: * @param os
162: */
163: public void writeTo(OutputStream os) {
164: }
165:
166: /**
167: * Writes out the header.
168: *
169: * @throws XMLStreamException
170: * if the operation fails for some reason. This leaves the
171: * writer to an undefined state.
172: */
173: public void writeTo(javax.xml.stream.XMLStreamWriter streamWriter)
174: throws javax.xml.stream.XMLStreamException {
175: JAXBElement<TimestampType> tsElem = new ObjectFactory()
176: .createTimestamp(this );
177: try {
178: // If writing to Zephyr, get output stream and use JAXB UTF-8 writer
179: if (streamWriter instanceof Map) {
180: OutputStream os = (OutputStream) ((Map) streamWriter)
181: .get("sjsxp-outputstream");
182: if (os != null) {
183: streamWriter.writeCharacters(""); // Force completion of open elems
184: getMarshaller().marshal(tsElem, os);
185: return;
186: }
187: }
188:
189: getMarshaller().marshal(tsElem, streamWriter);
190: } catch (JAXBException e) {
191: throw new XMLStreamException(e);
192: }
193: }
194:
195: private Marshaller getMarshaller() throws JAXBException {
196: return JAXBUtil.createMarshaller(soapVersion);
197: }
198:
199: /*
200: * The <wsu:Created> element specifies a timestamp used to
201: * indicate the creation time. It is defined as part of the
202: * <wsu:Timestamp> definition.
203: *
204: * Time reference in WSS work should be in terms of
205: * dateTime type specified in XML Schema in UTC time(Recommmended)
206: */
207: public void createDateTime() throws XWSSecurityException {
208: if (created == null) {
209: Calendar c = new GregorianCalendar();
210: int offset = c.get(Calendar.ZONE_OFFSET);
211: if (c.getTimeZone().inDaylightTime(c.getTime())) {
212: offset += c.getTimeZone().getDSTSavings();
213: }
214: synchronized (calendarFormatter1) {
215: calendarFormatter1.setTimeZone(c.getTimeZone());
216:
217: // always send UTC/GMT time
218: long beforeTime = c.getTimeInMillis();
219: long currentTime = 0;
220: currentTime = beforeTime - offset;
221: c.setTimeInMillis(currentTime);
222: // finished UTC/GMT adjustment
223:
224: setCreated(calendarFormatter1.format(c.getTime()));
225:
226: c.setTimeInMillis(currentTime + timeout);
227: setExpires(calendarFormatter1.format(c.getTime()));
228: }
229: }
230: }
231:
232: /**
233: *
234: * @param id
235: * @return
236: */
237: public boolean refersToSecHdrWithId(String id) {
238: return false;
239: }
240:
241: /**
242: *
243: * @param streamWriter
244: * @param props
245: * @throws javax.xml.stream.XMLStreamException
246: */
247: public void writeTo(javax.xml.stream.XMLStreamWriter streamWriter,
248: HashMap props) throws javax.xml.stream.XMLStreamException {
249: try {
250: Marshaller marshaller = getMarshaller();
251: Iterator<Map.Entry<Object, Object>> itr = props.entrySet()
252: .iterator();
253: while (itr.hasNext()) {
254: Map.Entry<Object, Object> entry = itr.next();
255: marshaller.setProperty((String) entry.getKey(), entry
256: .getValue());
257: }
258: writeTo(streamWriter);
259: } catch (JAXBException jbe) {
260: throw new XMLStreamException(jbe);
261: }
262: }
263:
264: public static final SimpleDateFormat calendarFormatter1 = new SimpleDateFormat(
265: "yyyy-MM-dd'T'HH:mm:ss'Z'");
266:
267: }
|