1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 package org.apache.ws.security.message;
21
22 import org.apache.ws.security.WSSConfig;
23 import org.apache.ws.security.message.token.Timestamp;
24 import org.apache.ws.security.util.WSSecurityUtil;
25 import org.w3c.dom.Document;
26 import org.w3c.dom.Element;
27
28 /**
29 * Builds a WS Timestamp and inserts it into the SOAP Envelope. Refer to the WS
30 * specification 1.0. chapter 10 / appendix A.2
31 *
32 * @author Christof Soehngen (Christof.Soehngen@syracom.de).
33 * @author Werner Dittmann (werner@apache.org).
34 */
35
36 public class WSSecTimestamp extends WSSecBase {
37 private static org.apache.commons.logging.Log log =
38 org.apache.commons.logging.LogFactory.getLog(WSSecTimestamp.class);
39
40 private Timestamp ts = null;
41
42 private int timeToLive = 300; // time between Created and Expires
43
44 public WSSecTimestamp() {
45 super();
46 }
47 public WSSecTimestamp(WSSConfig config) {
48 super(config);
49 }
50 /**
51 * Set the time to live. This is the time difference in seconds between the
52 * <code>Created</code> and the <code>Expires</code> in
53 * <code>Timestamp</code>. <p/>
54 *
55 * @param ttl The time to live in second
56 */
57 public void setTimeToLive(int ttl) {
58 timeToLive = ttl;
59 }
60
61 /**
62 * Creates a Timestamp element.
63 *
64 * The method prepares and initializes a WSSec Timestamp structure after the
65 * relevant information was set. Before calling <code>prepare()</code> the
66 * parameter such as <code>timeToLive</code> can be set if the default
67 * value is not suitable.
68 *
69 * @param doc The SOAP envelope as W3C document
70 */
71 public void prepare(Document doc) {
72 ts = new Timestamp(getWsConfig().isPrecisionInMilliSeconds(), doc, timeToLive);
73 String tsId = getWsConfig().getIdAllocator().createId("TS-", ts);
74 ts.setID(tsId);
75 }
76
77 /**
78 * Prepends the Timestamp element to the elements already in the Security
79 * header.
80 *
81 * The method can be called any time after <code>prepare()</code>. This
82 * allows to insert the Timestamp element at any position in the Security
83 * header.
84 *
85 * @param secHeader The security header that holds the Signature element.
86 */
87 public void prependToHeader(WSSecHeader secHeader) {
88 WSSecurityUtil.prependChildElement(secHeader.getSecurityHeader(), ts.getElement());
89 }
90
91 /**
92 * Adds a new <code>Timestamp</code> to a soap envelope.
93 *
94 * A complete <code>Timestamp</code> is constructed and added to the
95 * <code>wsse:Security</code> header.
96 *
97 * @param doc The SOAP envelope as W3C document
98 * @param secHeader The security header that hold this Timestamp
99 * @return Document with Timestamp added
100 * @throws Exception
101 */
102 public Document build(Document doc, WSSecHeader secHeader) {
103 log.debug("Begin add timestamp...");
104
105 prepare(doc);
106 prependToHeader(secHeader);
107
108 return doc;
109 }
110
111 /**
112 * Get the id generated during <code>prepare()</code>.
113 *
114 * Returns the the value of wsu:Id attribute of this Timestamp.
115 *
116 * @return Return the wsu:Id of this token or null if
117 * <code>prepareToken()</code> was not called before.
118 */
119 public String getId() {
120 if (ts == null) {
121 return null;
122 }
123 return ts.getID();
124 }
125
126 /**
127 * Get the timestamp element generated during <code>prepare()</code>.
128 */
129 public Element getElement() {
130 if (ts == null) {
131 return null;
132 }
133 return ts.getElement();
134 }
135 }