This project has retired. For details please refer to its Attic page.
WssTube xref

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  package org.apache.chemistry.opencmis.commons.impl.tube.server;
20  
21  import java.text.SimpleDateFormat;
22  import java.util.TimeZone;
23  
24  import javax.xml.parsers.DocumentBuilderFactory;
25  
26  import org.apache.chemistry.opencmis.commons.impl.tube.AbstractWssTube;
27  import org.w3c.dom.Document;
28  import org.w3c.dom.Element;
29  
30  import com.sun.xml.ws.api.message.HeaderList;
31  import com.sun.xml.ws.api.message.Headers;
32  import com.sun.xml.ws.api.message.Message;
33  import com.sun.xml.ws.api.message.Packet;
34  import com.sun.xml.ws.api.pipe.NextAction;
35  import com.sun.xml.ws.api.pipe.Tube;
36  import com.sun.xml.ws.api.pipe.TubeCloner;
37  import com.sun.xml.ws.api.pipe.helper.AbstractFilterTubeImpl;
38  
39  public class WssTube extends AbstractWssTube {
40  
41      public WssTube(Tube next) {
42          super(next);
43      }
44  
45      protected WssTube(AbstractFilterTubeImpl that, TubeCloner cloner) {
46          super(that, cloner);
47      }
48  
49      public WssTube copy(TubeCloner cloner) {
50          return new WssTube(this, cloner);
51      }
52  
53      @Override
54      public NextAction processResponse(Packet packet) {
55          Message message = packet.getMessage();
56          if (message == null) {
57              return super.processResponse(packet);
58          }
59  
60          try {
61              SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
62              sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
63              long created = System.currentTimeMillis();
64              long expires = created + 24 * 60 * 60 * 1000; // 24 hours
65  
66              Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
67  
68              Element wsseSecurityElement = document.createElementNS(WSSE_NAMESPACE, "Security");
69  
70              Element wsuTimestampElement = document.createElementNS(WSU_NAMESPACE, "Timestamp");
71              wsseSecurityElement.appendChild(wsuTimestampElement);
72  
73              Element tsCreatedElement = document.createElementNS(WSU_NAMESPACE, "Created");
74              tsCreatedElement.setTextContent(sdf.format(created));
75              wsuTimestampElement.appendChild(tsCreatedElement);
76  
77              Element tsExpiresElement = document.createElementNS(WSU_NAMESPACE, "Expires");
78              tsExpiresElement.setTextContent(sdf.format(expires));
79              wsuTimestampElement.appendChild(tsExpiresElement);
80  
81              HeaderList headers = message.getHeaders();
82              headers.add(Headers.create(wsseSecurityElement));
83          } catch (Exception e) {
84              e.printStackTrace();
85          }
86  
87          return super.processResponse(packet);
88      }
89  }