
org.xlcloud.console.controllers.utils.RcFileBuilder Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2012 AMG.lab, a Bull Group Company
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.xlcloud.console.controllers.utils;
import java.io.IOException;
import java.io.InputStream;
import java.util.regex.Pattern;
import javax.inject.Inject;
import javax.inject.Named;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import org.xlcloud.service.OsCredentials;
import org.xlcloud.service.api.OsCredentialsApi;
/**
* Class which is used to build rc-files from the template file.
* @author Konrad Król, AMG.net
*/
@Named
public class RcFileBuilder {
public static final String OS_AUTH_URL_ELEMENT = "#{OS_AUTH_URL}";
public static final String OS_PROJECT_ID_ELEMENT = "#{OS_TENANT_ID}";
public static final String OS_PROJECT_NAME_ELEMENT = "#{OS_TENANT_NAME}";
public static final String OS_USERNAME_ELEMENT = "#{OS_USERNAME}";
public static final String OPENRC_TEMPLATE_RESOURCE_PATH = "file-templates/openrc.sh";
public static final String TARGET_FILE_NAME = "openrc.sh";
@Inject
private OsCredentialsApi osCredentialsApi;
/**
* It builds rc-file from the template localized under {@link RcFileBuilder#OPENRC_TEMPLATE_RESOURCE_PATH}.
* Os-credentials are retrieved from the XMS api method: {@link OsCredentialsApi#getOsCredentials(Long, Long)}.
*
* @param userId id of the user whose os-credentials should be retrieved
* @param projectId id of the project
* @return streamed content with complete rc-file
* @throws IOException if template could not be read
*/
public StreamedContent buildRcFile(Long userId, Long projectId) throws IOException {
InputStream rcTemplateFile = this.getClass().getClassLoader().getResourceAsStream(OPENRC_TEMPLATE_RESOURCE_PATH);
String rcFileContent = IOUtils.toString(rcTemplateFile);
OsCredentials osCredentials = osCredentialsApi.getOsCredentials(userId, projectId);
rcFileContent = replaceTemplate(rcFileContent, OS_AUTH_URL_ELEMENT, osCredentials.getAuthUrl());
rcFileContent = replaceTemplate(rcFileContent, OS_PROJECT_ID_ELEMENT, osCredentials.getOsProjectId());
rcFileContent = replaceTemplate(rcFileContent, OS_PROJECT_NAME_ELEMENT, osCredentials.getOsProjectName());
rcFileContent = replaceTemplate(rcFileContent, OS_USERNAME_ELEMENT, osCredentials.getUsername());
InputStream stream = IOUtils.toInputStream(rcFileContent);
return new DefaultStreamedContent(stream, "application/octet-stream", TARGET_FILE_NAME);
}
private static String replaceTemplate(String template, String element, String replacement) {
return template.replaceAll(Pattern.quote(element), StringUtils.defaultString(replacement));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy