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.wss4j.dom.message;
21
22 import org.w3c.dom.Document;
23 import org.w3c.dom.Element;
24
25 import org.apache.wss4j.common.util.WSCurrentTimeSource;
26 import org.apache.wss4j.common.util.WSTimeSource;
27 import org.apache.wss4j.dom.message.token.Timestamp;
28 import org.apache.wss4j.dom.util.WSSecurityUtil;
29
30 /**
31 * Builds a WS Timestamp and inserts it into the SOAP Envelope. Refer to the WS
32 * specification 1.0. chapter 10 / appendix A.2
33 */
34
35 public class WSSecTimestamp extends WSSecBase {
36 private static final org.slf4j.Logger LOG =
37 org.slf4j.LoggerFactory.getLogger(WSSecTimestamp.class);
38
39 private Timestamp ts;
40
41 private int timeToLive = 300; // time between Created and Expires
42 private boolean precisionInMilliSeconds = true;
43 private WSTimeSource wsTimeSource = new WSCurrentTimeSource();
44
45 public WSSecTimestamp(WSSecHeader securityHeader) {
46 super(securityHeader);
47 }
48
49 public WSSecTimestamp(Document doc) {
50 super(doc);
51 }
52
53 /**
54 * Set the time to live. This is the time difference in seconds between the
55 * <code>Created</code> and the <code>Expires</code> in
56 * <code>Timestamp</code>. <p/>
57 *
58 * @param ttl The time to live in second
59 */
60 public void setTimeToLive(int ttl) {
61 timeToLive = ttl;
62 }
63
64 /**
65 * Creates a Timestamp element.
66 *
67 * The method prepares and initializes a WSSec Timestamp structure after the
68 * relevant information was set. Before calling <code>prepare()</code> the
69 * parameter such as <code>timeToLive</code> can be set if the default
70 * value is not suitable.
71 */
72 public void prepare() {
73 ts = new Timestamp(precisionInMilliSeconds, getDocument(), wsTimeSource, timeToLive);
74 String tsId = getIdAllocator().createId("TS-", ts);
75 ts.setID(tsId);
76 if (addWSUNamespace) {
77 ts.addWSUNamespace();
78 }
79 }
80
81 /**
82 * Prepends the Timestamp element to the elements already in the Security
83 * header.
84 *
85 * The method can be called any time after <code>prepare()</code>. This
86 * allows to insert the Timestamp element at any position in the Security
87 * header.
88 */
89 public void prependToHeader() {
90 Element securityHeaderElement = getSecurityHeader().getSecurityHeaderElement();
91 WSSecurityUtil.prependChildElement(securityHeaderElement, ts.getElement());
92 }
93
94 /**
95 * Adds a new <code>Timestamp</code> to a soap envelope.
96 *
97 * A complete <code>Timestamp</code> is constructed and added to the
98 * <code>wsse:Security</code> header.
99 *
100 * @return Document with Timestamp added
101 * @throws Exception
102 */
103 public Document build() {
104 LOG.debug("Begin add timestamp...");
105
106 prepare();
107 prependToHeader();
108
109 return getDocument();
110 }
111
112 /**
113 * Get the id generated during <code>prepare()</code>.
114 *
115 * Returns the the value of wsu:Id attribute of this Timestamp.
116 *
117 * @return Return the wsu:Id of this token or null if
118 * <code>prepareToken()</code> was not called before.
119 */
120 public String getId() {
121 if (ts == null) {
122 return null;
123 }
124 return ts.getID();
125 }
126
127 public WSTimeSource getWsTimeSource() {
128 return wsTimeSource;
129 }
130
131 public void setWsTimeSource(WSTimeSource wsTimeSource) {
132 this.wsTimeSource = wsTimeSource;
133 }
134
135 /**
136 * Get the timestamp element generated during <code>prepare()</code>.
137 */
138 public Element getElement() {
139 if (ts == null) {
140 return null;
141 }
142 return ts.getElement();
143 }
144
145 public boolean isPrecisionInMilliSeconds() {
146 return precisionInMilliSeconds;
147 }
148
149 public void setPrecisionInMilliSeconds(boolean precisionInMilliSeconds) {
150 this.precisionInMilliSeconds = precisionInMilliSeconds;
151 }
152 }