001package ca.uhn.fhir.tls;
002
003/*-
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2023 Smile CDR, Inc.
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import ca.uhn.fhir.i18n.Msg;
024import org.apache.commons.io.FilenameUtils;
025
026import static org.apache.commons.lang3.StringUtils.isBlank;
027
028public abstract class BaseStoreInfo {
029
030        private final String myFilePath;
031        private final PathType myPathType;
032        private final char[] myStorePass;
033        private final String myAlias;
034        private final KeyStoreType myType;
035
036        public BaseStoreInfo(String theFilePath, String theStorePass, String theAlias) {
037                if(theFilePath.startsWith(PathType.RESOURCE.getPrefix())){
038                        myFilePath = theFilePath.substring(PathType.RESOURCE.getPrefix().length());
039                        myPathType = PathType.RESOURCE;
040                }
041                else if(theFilePath.startsWith(PathType.FILE.getPrefix())){
042                        myFilePath = theFilePath.substring(PathType.FILE.getPrefix().length());
043                        myPathType = PathType.FILE;
044                }
045                else {
046                        throw new StoreInfoException(Msg.code(2117)+"Invalid path prefix");
047                }
048
049                myStorePass = toCharArray(theStorePass);
050                myAlias = theAlias;
051
052                String extension = FilenameUtils.getExtension(theFilePath);
053                myType = KeyStoreType.fromFileExtension(extension);
054        }
055
056        public String getFilePath() {
057                return myFilePath;
058        }
059
060        public char[] getStorePass() {
061                return myStorePass;
062        }
063
064        public String getAlias() {
065                return myAlias;
066        }
067
068        public KeyStoreType getType() {
069                return myType;
070        }
071
072        public PathType getPathType() {
073                return myPathType;
074        }
075
076        protected char[] toCharArray(String theString){
077                return isBlank(theString) ? "".toCharArray() : theString.toCharArray();
078        }
079
080        public static class StoreInfoException extends RuntimeException {
081                private static final long serialVersionUID = 1l;
082                public StoreInfoException(String theMessage) {
083                        super(theMessage);
084                }
085        }
086}