001/**
002 * Copyright 2005-2015 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.ksb.security;
017
018import java.security.Signature;
019import java.security.cert.Certificate;
020
021import javax.servlet.http.HttpServletResponse;
022
023import org.apache.commons.lang.StringUtils;
024import org.kuali.rice.ksb.util.KSBConstants;
025
026/**
027 * A DigitalSinger which places the alias and digital signature into the response headers of an HttpServletResponse.
028 * 
029 * @author Kuali Rice Team (rice.collab@kuali.org)
030 */
031public class ResponseHeaderDigitalSigner extends AbstractDigitalSigner {
032
033        private String alias;
034        private Certificate certificate;
035        private HttpServletResponse response;
036        
037    public ResponseHeaderDigitalSigner(Signature signature, String alias, HttpServletResponse response) {
038        super(signature);
039        this.alias = alias;
040        this.response = response;
041    }
042    
043    public ResponseHeaderDigitalSigner(Signature signature, String alias, Certificate certificate, HttpServletResponse response) {
044        this(signature, alias, response);
045        this.certificate = certificate;
046    }
047    
048    public ResponseHeaderDigitalSigner(Signature signature, Certificate certificate, HttpServletResponse response) {
049        super(signature);
050        this.certificate = certificate;
051        this.response = response;
052    }
053    
054        public void sign() throws Exception {
055            if (StringUtils.isNotBlank(this.alias) ) {
056                this.response.setHeader(KSBConstants.KEYSTORE_ALIAS_HEADER, this.alias);
057            }
058            if (this.certificate != null) {
059                this.response.setHeader(KSBConstants.KEYSTORE_CERTIFICATE_HEADER, getEncodedCertificate(this.certificate));
060            }
061            this.response.setHeader(KSBConstants.DIGITAL_SIGNATURE_HEADER, getEncodedSignature());
062        }
063
064}