TimestampMessageProcessor.java

  1. /*
  2.  * AdroitLogic UltraESB Enterprise Service Bus
  3.  *
  4.  * Copyright (c) 2010-2012 AdroitLogic Private Ltd. (http://adroitlogic.org). All Rights Reserved.
  5.  *
  6.  * GNU Affero General Public License Usage
  7.  *
  8.  * This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General
  9.  * Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option)
  10.  * any later version.
  11.  *
  12.  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
  13.  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for
  14.  * more details.
  15.  *
  16.  * You should have received a copy of the GNU Affero General Public License along with this program (See LICENSE-AGPL.TXT).
  17.  * If not, see http://www.gnu.org/licenses/agpl-3.0.html
  18.  *
  19.  * Commercial Usage
  20.  *
  21.  * Licensees holding valid UltraESB Commercial licenses may use this file in accordance with the UltraESB Commercial
  22.  * License Agreement provided with the Software or, alternatively, in accordance with the terms contained in a written
  23.  * agreement between you and AdroitLogic.
  24.  *
  25.  * If you are unsure which license is appropriate for your use, or have questions regarding the use of this file,
  26.  * please contact AdroitLogic at info@adroitlogic.com
  27.  */
  28. /*
  29.  * Modificato da Link.it (https://link.it) per supportare le seguenti funzionalità:
  30.  * - firma e cifratura degli attachments
  31.  * - cifratura con chiave simmetrica
  32.  * - supporto CRL
  33.  *
  34.  * Copyright (c) 2011-2025 Link.it srl (https://link.it).
  35.  *
  36.  */

  37. package org.openspcoop2.security.message.soapbox;

  38. import java.util.Calendar;
  39. import java.util.Date;
  40. import java.util.GregorianCalendar;
  41. import java.util.TimeZone;

  42. import org.adroitlogic.soapbox.CryptoUtil;
  43. import org.adroitlogic.soapbox.InvalidMessageDataException;
  44. import org.adroitlogic.soapbox.MessageSecurityContext;
  45. import org.adroitlogic.soapbox.Processor;
  46. import org.adroitlogic.soapbox.SBConstants;
  47. import org.adroitlogic.soapbox.SecurityConfig;
  48. import org.apache.commons.lang.time.FastDateFormat;
  49. import org.openspcoop2.utils.UtilsException;
  50. import org.openspcoop2.utils.date.DateManager;
  51. import org.w3c.dom.Document;
  52. import org.w3c.dom.Element;

  53. /**
  54.  * TimestampMessageProcessor
  55.  *
  56.  * Author of the original AdroitLogic code:
  57.  * @author asankha
  58.  *
  59.  * Authors of the Link.it modification to the code:
  60.  * @author Andrea Poli (apoli@link.it)
  61.  * @author Giovanni Bussu (bussu@link.it)
  62.  * @author $Author$
  63.  * @version $Rev$, $Date$
  64.  */
  65. public class TimestampMessageProcessor implements Processor {

  66.     private final FastDateFormat zulu;
  67.    
  68.     public TimestampMessageProcessor() {
  69.         this.zulu = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", TimeZone.getTimeZone("UTC"));
  70.     }
  71.     @Override
  72.     public void process(SecurityConfig secConfig, MessageSecurityContext msgSecCtx) {
  73.         // ensure existence of the wsse:Security header, and create one if none exists
  74.         Document doc = msgSecCtx.getDocument();
  75.         Element wsseSecurityElem = CryptoUtil.getWSSecurityHeader(doc);

  76.         // we will not timestamp an already timestamped document
  77.         if (CryptoUtil.getFirstChildOrNull(wsseSecurityElem, SBConstants.WSU, "Timestamp") != null) {
  78.             throw new InvalidMessageDataException("Message is already timestamped");
  79.         }

  80.         Element timestampElem = doc.createElementNS(SBConstants.WSU, "wsu:Timestamp");
  81.         CryptoUtil.setWsuId(timestampElem, CryptoUtil.getRandomId());
  82.         Element createdElem = doc.createElementNS(SBConstants.WSU, "wsu:Created");
  83.         Element expiresElem = doc.createElementNS(SBConstants.WSU, "wsu:Expires");

  84.         long ttl = msgSecCtx.getTimestampRequest().getTimeForExpiryMillis();
  85.         Calendar currentTime = null;
  86.         Calendar expiryTime = null;
  87.         try {
  88.             currentTime = DateManager.getCalendar();
  89.         } catch (UtilsException e) {
  90.             currentTime = new GregorianCalendar();
  91.             currentTime.setTimeInMillis(new Date().getTime());
  92.         }

  93.         expiryTime = new GregorianCalendar();
  94.         expiryTime.setTimeInMillis(currentTime.getTimeInMillis() + ttl);
  95.        
  96.         synchronized (this.zulu) {
  97.             createdElem.setTextContent(this.zulu.format(currentTime.getTime()));
  98.             expiresElem.setTextContent(this.zulu.format(expiryTime.getTime()));
  99.         }
  100.         timestampElem.appendChild(createdElem);
  101.         timestampElem.appendChild(expiresElem);
  102.        
  103.         Element firstChild = CryptoUtil.getFirstElementChild(wsseSecurityElem);
  104.         if (firstChild != null) {
  105.             wsseSecurityElem.insertBefore(timestampElem, firstChild);
  106.         } else {
  107.             wsseSecurityElem.appendChild(timestampElem);
  108.         }
  109.     }


  110. }