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