SSLHostConfigSF.java

  1. /**
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.catalina.storeconfig;

  18. import java.io.PrintWriter;
  19. import java.util.ArrayList;

  20. import org.apache.juli.logging.Log;
  21. import org.apache.juli.logging.LogFactory;
  22. import org.apache.tomcat.util.net.SSLHostConfig;
  23. import org.apache.tomcat.util.net.SSLHostConfigCertificate;
  24. import org.apache.tomcat.util.net.SSLHostConfigCertificate.Type;
  25. import org.apache.tomcat.util.net.openssl.OpenSSLConf;

  26. /**
  27.  * Store SSLHostConfig
  28.  */
  29. public class SSLHostConfigSF extends StoreFactoryBase {

  30.     private static Log log = LogFactory.getLog(SSLHostConfigSF.class);

  31.     @Override
  32.     public void store(PrintWriter aWriter, int indent, Object aElement) throws Exception {
  33.         StoreDescription elementDesc = getRegistry().findDescription(aElement.getClass());
  34.         if (elementDesc != null) {
  35.             if (log.isTraceEnabled()) {
  36.                 log.trace(sm.getString("factory.storeTag", elementDesc.getTag(), aElement));
  37.             }
  38.             getStoreAppender().printIndent(aWriter, indent + 2);
  39.             aWriter.print("<");
  40.             aWriter.print(elementDesc.getTag());
  41.             if (elementDesc.isAttributes()) {
  42.                 // Add protocols attribute
  43.                 SSLHostConfig bean2 = (SSLHostConfig) getStoreAppender().defaultInstance(aElement);
  44.                 SSLHostConfig sslHostConfig = (SSLHostConfig) aElement;
  45.                 if (!bean2.getProtocols().equals(sslHostConfig.getProtocols())) {
  46.                     StringBuffer protocolsValue = new StringBuffer();
  47.                     for (String protocol : sslHostConfig.getProtocols()) {
  48.                         protocolsValue.append('+').append(protocol);
  49.                     }
  50.                     getStoreAppender().printValue(aWriter, indent, "protocols", protocolsValue.toString());
  51.                 }
  52.                 getStoreAppender().printAttributes(aWriter, indent, aElement, elementDesc);
  53.             }
  54.             aWriter.println(">");
  55.             storeChildren(aWriter, indent + 2, aElement, elementDesc);
  56.             getStoreAppender().printIndent(aWriter, indent + 2);
  57.             getStoreAppender().printCloseTag(aWriter, elementDesc);
  58.         } else {
  59.             if (log.isWarnEnabled()) {
  60.                 log.warn(sm.getString("factory.storeNoDescriptor", aElement.getClass()));
  61.             }
  62.         }
  63.     }

  64.     /**
  65.      * Store nested SSLHostConfigCertificate elements.
  66.      * <p>
  67.      * {@inheritDoc}
  68.      */
  69.     @Override
  70.     public void storeChildren(PrintWriter aWriter, int indent, Object aSSLHostConfig, StoreDescription parentDesc)
  71.             throws Exception {
  72.         if (aSSLHostConfig instanceof SSLHostConfig) {
  73.             SSLHostConfig sslHostConfig = (SSLHostConfig) aSSLHostConfig;
  74.             // Store nested <SSLHostConfigCertificate> elements
  75.             SSLHostConfigCertificate[] hostConfigsCertificates =
  76.                     sslHostConfig.getCertificates().toArray(new SSLHostConfigCertificate[0]);
  77.             // Remove a possible default UNDEFINED certificate
  78.             if (hostConfigsCertificates.length > 1) {
  79.                 ArrayList<SSLHostConfigCertificate> certificates = new ArrayList<>();
  80.                 for (SSLHostConfigCertificate certificate : hostConfigsCertificates) {
  81.                     if (Type.UNDEFINED != certificate.getType()) {
  82.                         certificates.add(certificate);
  83.                     }
  84.                 }
  85.                 hostConfigsCertificates = certificates.toArray(new SSLHostConfigCertificate[0]);
  86.             }
  87.             storeElementArray(aWriter, indent, hostConfigsCertificates);
  88.             // Store nested <OpenSSLConf> element
  89.             OpenSSLConf openSslConf = sslHostConfig.getOpenSslConf();
  90.             storeElement(aWriter, indent, openSslConf);
  91.         }
  92.     }

  93. }