/*
* Copyright 2013 Cloud4SOA, www.cloud4soa.eu
*
* 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 eu.cloud4soa.soa.git;
import eu.cloud4soa.api.datamodel.soa.GitRepoInfo;
import eu.cloud4soa.api.util.exception.soa.SOAException;
import eu.cloud4soa.relational.persistence.*;
import eu.cloud4soa.relational.datamodel.*;
import eu.cloud4soa.soa.git.utils.Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
import java.io.File;
import java.io.InputStream;
import java.util.List;
import javax.ws.rs.core.Response;
import org.springframework.beans.factory.annotation.Value;
@Transactional
@Component
public class GitServices extends SpringBeanAutowiringSupport {
final Logger logger = LoggerFactory.getLogger(GitServices.class);
@Autowired
PubKeyRepository pubkeydao;
@Autowired
UserRepository userdao;
@Autowired
GitRepoRepository repodao;
@Autowired
GitProxyRepository proxydao;
@Autowired
PaasRepository paasdao;
@Autowired
ApplicationInstanceRepository appdao;
@Autowired
AccountRepository accountdao;
//the first time the file has to be generated by ssh-agent
//Properties loaded by using Spring PropertyPlaceholderConfigurer
@Value("GIT{git.keys.authorized}")
protected String AUTHORIZED_KEYS_FILE = ""; //System.getProperty("user.home")+File.separator+".ssh"+File.separator+"authorized_keys";
@Value("GIT{git.keys.publicKey}")
protected String C4SOA_SERVER_PUBLIC_KEY = ""; //System.getProperty("user.home")+File.separator+".ssh"+File.separator+"id_rsa.pub";
@Value("GIT{git.proxyFile}")
protected String PROXY_GIT_FILE = ""; //System.getProperty("user.home")+File.separator+"proxy-git";
@Value("GIT{git.server.accountName}")
protected String SERVER_ACCOUNT_NAME = ""; //"cloud";
@Value("GIT{git.server.address}")
protected String SERVER_IP_ADDRESS = ""; //"127.0.0.1";
public GitServices(){
}
/*
* Key Handling Methods
*/
public String[] getC4SOAPublicKey() {
String[] res = {"1", "Internal Server Error"};
try {
String pub = Util.readfile(C4SOA_SERVER_PUBLIC_KEY);
logger.info("Success-Public Key Fetched ");
res = new String[]{"0", pub};
} catch (Exception e) {
logger.error("Error reading public key file");
res = new String[]{"1", "Error reading public key file"};
}
return res;
}
public String[] generateKeyPairForUser(String userInstanceUriId) {
String[] res = {"1","Internal Server Error"};
int retvalue=0;
if (userdao != null) {
List userlist = userdao.findBy("uriID", userInstanceUriId);
if (userlist != null && !userlist.isEmpty()) {
//Step 1 - Execute command
try {
Process child = Runtime.getRuntime().exec(new String[] {"ssh-keygen", //
"-q" , // quiet
"-t", "rsa" , //
"-P", "" , // Zero password
"-C", userInstanceUriId , //
"-f", userInstanceUriId //
});
child.waitFor();
//Get the input stream and read from it
InputStream in = child.getInputStream();
int c;
while ((c = in.read()) != -1) {
//System.out.print((char)c);
}
in.close();
retvalue = child.exitValue();
logger.info("ssh-keygen returned: "+ retvalue);
//Step 2 - read file
if (retvalue == 0){ //Everything OK
try{
String pub,priv="";
pub = Util.readfile(userInstanceUriId + ".pub");
priv = Util.readfile(userInstanceUriId);
//cleanup
try {
String command1 = "rm "+userInstanceUriId+".pub";
Process child1 = Runtime.getRuntime().exec(command1);
String command2 = "rm "+userInstanceUriId;
Process child2 = Runtime.getRuntime().exec(command2);
child1.waitFor();
child2.waitFor();
logger.info("Success-Key-Pair created for User with username: " );
res = new String[]{"0",pub,priv};
} catch (Exception ex) {
logger.error("Error-Cleaning temporary files");
res = new String[]{"1","Error-Cleaning temporary files"};
}
} catch (Exception ex){
logger.error("Error-Reading temporary files");
res = new String[]{"1","Error-Reading temporary files"};
}
} else {
logger.error("Error-Internal ssh-keygen error");
res = new String[]{"1","Internal ssh-keygen error"};
}
} catch (Exception ex) {
logger.error("Error-Internal Server Error while executing ssh-keygen");
res = new String[]{"1","Error-Internal Server Error while executing ssh-keygen"};
}
} else {
logger.error("User with userInstanceUriId: "+userInstanceUriId+" does not exist");
res = new String[]{"1","Error-User with userInstanceUriId: "+userInstanceUriId+" does not exist"};
}
} else {
logger.error("SpringBean userdao cannot be instantiated");
res = new String[]{"1","Error-Internal Server - Spring Bean cannot be initialized"};
}
return res;
}
/*
* Key Handling Methods
*/
public String[] registerPublicKeyForUser(String userInstanceUriId, String rsa_pub_key) {
String[] res = {"1","Internal Server Error"};
if (userdao != null) {
List userlist = userdao.findBy("uriID", userInstanceUriId);
if (userlist != null && !userlist.isEmpty()) {
User user = (User) userlist.get(0);
List<PubKey> pubkeys = pubkeydao.findByUserAndPubkey(user, rsa_pub_key);
if (pubkeys.isEmpty()){
PubKey pubKey = new PubKey();
pubKey.setPubkey(rsa_pub_key);
pubKey.setUser(user);
pubkeydao.save(pubKey);
//write to file
String segment = Util.createAuthorizedKeysSegment(PROXY_GIT_FILE,pubKey.getId()+"",rsa_pub_key);
Util.appendToFile(AUTHORIZED_KEYS_FILE,segment);
logger.info("Success-User with userInstanceUriId: " + userInstanceUriId + " registered pubkey");
res = new String[]{"0","OK"};
} else {
logger.error("User with userInstanceUriId: "+userInstanceUriId+" already contains the key");
res = new String[]{"1","Error-User with userInstanceUriId: "+userInstanceUriId+" already contains the key"};
}
} else {
logger.error("User with userInstanceUriId: "+userInstanceUriId+" does not exist");
res = new String[]{"1","Error-User with userInstanceUriId: "+userInstanceUriId+" does not exist"};
}
} else {
logger.error("SpringBean userdao cannot be instantiated");
res = new String[]{"1","Error-Internal Server - Spring Bean cannot be initialized"};
}
return res;
}
public String[] deletePublicKeyFromUser(String userInstanceUriId, String rsa_pub_key) {
String[] res = {"1","Internal Server Error"};
if (userdao != null) {
List userlist = userdao.findBy("uriID", userInstanceUriId);
if (userlist != null && !userlist.isEmpty()) {
User user = (User) userlist.get(0);
try{
List<PubKey> pubkeys = pubkeydao.findByUserAndPubkey(user,rsa_pub_key);
if (!pubkeys.isEmpty()){
//delete them from file first
for (int i = 0; i < pubkeys.size(); i++) {
PubKey pubKey = pubkeys.get(i);
Util.replaceBlockWithSedForAuthorizedKeys(AUTHORIZED_KEYS_FILE, rsa_pub_key);
pubkeydao.delete(pubKey);
}
//delete them from the database
logger.info("Success-Deleted pubkey for user with userInstanceUriId: " + userInstanceUriId );
res = new String[]{"0","OK"};
} else {
logger.error("User with userInstanceUriId: "+userInstanceUriId+" does not contain the specific pubkey");
res = new String[]{"1","User with userInstanceUriId: "+userInstanceUriId+" does not contain the specific pubkey"};
}
} catch(Exception ex){
logger.error("Error-Internal Server Error "+ex);
res = new String[]{"1","Error-Internal Server Error "+ex};
}
} else {
logger.error("User with userInstanceUriId: "+userInstanceUriId+" does not exist");
res = new String[]{"1","Error-User with userInstanceUriId: "+userInstanceUriId+" does not exist"};
}
} else {
logger.error("SpringBean userdao cannot be instantiated");
res = new String[]{"1","Error-Internal Server - Spring Bean cannot be initialized"};
}
return res;
}
public List<PubKey> getPublicKeysForUser(String userInstanceUriId) throws SOAException {
if (userdao != null) {
List userlist = userdao.findBy("uriID", userInstanceUriId);
if (userlist != null && !userlist.isEmpty()) {
User user = (User) userlist.get(0);
List<PubKey> pubkeys = pubkeydao.findByUser(user);
return pubkeys;
} else {
logger.error("User with userInstanceUriId: "+userInstanceUriId+" does not exist");
throw new SOAException(Response.Status.BAD_REQUEST, "Error-User with userInstanceUriId: "+userInstanceUriId+" does not exist");
}
} else {
logger.error("SpringBean userdao cannot be instantiated");
throw new SOAException(Response.Status.INTERNAL_SERVER_ERROR, "Error-Internal Server - Spring Bean cannot be initialized");
}
}
// public String[] getPublicKeysForUser(String userInstanceUriId) {
// String[] res = {"1","Internal Server Error"};
// if (userdao != null) {
// List userlist = userdao.findBy("uriID", userInstanceUriId);
// if (userlist != null && !userlist.isEmpty()) {
// User user = (User) userlist.get(0);
// List<PubKey> pubkeys = pubkeydao.findByUser(user);
// logger.info("Success-User with userInstanceUriId: " + userInstanceUriId + " has "+pubkeys.size()+" keys");
// String ret="0";
// for (int i = 0; i < pubkeys.size(); i++) {
// PubKey pubKey = pubkeys.get(i);
// //in the first one we have to append a comma in after the "0"
// ret+=","+pubKey.getId()+","+pubKey.getPubkey();
// }
// res = ret.split(",");
// } else {
// logger.error("User with userInstanceUriId: "+userInstanceUriId+" does not exist");
// res = new String[]{"1","Error-User with userInstanceUriId: "+userInstanceUriId+" does not exist"};
// }
// } else {
// logger.error("SpringBean userdao cannot be instantiated");
// res = new String[]{"1","Error-Internal Server - Spring Bean cannot be initialized"};
// }
// return res;
// }
/*
* Repo Handling Methods
*/
public String[] registerGitRepository(String applicationInstanceUriId, String userInstanceUriId, String giturl, String reponame, String paaSInstanceUriId) {
Paas paas = null;
ApplicationInstance app = null;
String[] res = {"1","Internal Server Error"};
if (userdao != null) {
List userlist = userdao.findBy("uriID", userInstanceUriId);
if (userlist != null && !userlist.isEmpty()) {
User user = (User) userlist.get(0);
logger.info("userid:"+user.getId());
List paaslist = paasdao.findBy("uriID", paaSInstanceUriId);
if (paaslist != null && !paaslist.isEmpty()) {
paas = (Paas) paaslist.get(0);
List accountlist= accountdao.retrieve(user.getId(), paas.getId());
Account account = (Account) accountlist.get(0);
logger.info("Retrieved paaSInstanceUriId: " + paaSInstanceUriId + " paasId:" + paas.getId());
//List applist = appdao.findByUriId(applicationInstanceUriId);
List applist = appdao.findByUriIDAndAccountId(applicationInstanceUriId,account.getId());
if (applist != null && !applist.isEmpty()) {
app = (ApplicationInstance) applist.get(0);
logger.info("Retrieved applicationInstanceUriId: " + applicationInstanceUriId + " app:" + app.getId());
} else {
logger.error("App with applicationInstanceUriId: " + applicationInstanceUriId + " does not exist");
res = new String[]{"1", "Error-App with applicationInstanceUriId: " + applicationInstanceUriId + " does not exist"};
}
} else {
logger.error("PaaS with paaSInstanceUriId: " + paaSInstanceUriId + " does not exist");
res = new String[]{"1", "Error-PaaS with paaSInstanceUriId: " + paaSInstanceUriId + " does not exist"};
}
//check double
//List<GitRepo> repos = repodao.findByGitrepo(reponame);
List<GitRepo> repos = repodao.findByGitrepoAndGitUrl(reponame, giturl);
if (repos.isEmpty()){
GitRepo gitrepo = new GitRepo();
gitrepo.setGiturl(giturl);
gitrepo.setGitrepo(reponame);
//gitrepo.setUser(user);
gitrepo.setUser(user);
//gitrepo.setPaas(paas);
gitrepo.setPaas(paas);
gitrepo.setApp(app);
repodao.save(gitrepo);
logger.info("Success-Repo registered" );
res = new String[]{"0","OK"};
} else {
logger.error("Error- This git-repository already exists");
res = new String[]{"1","Error- This git-repository already exists"};
}
} else {
logger.error("User with userInstanceUriId: "+userInstanceUriId+" does not exist");
res = new String[]{"1","Error-User with userInstanceUriId: "+userInstanceUriId+" does not exist"};
}
} else {
logger.error("SpringBean userdao cannot be instantiated");
res = new String[]{"1","Error-Internal Server - Spring Bean cannot be initialized"};
}
return res;
}
public String[] registerGitProxy(String userInstanceUriId, String proxyname ) {
String[] res = {"1","Internal Server Error"};
if (userdao != null) {
List userlist = userdao.findBy("uriID", userInstanceUriId);
if (userlist != null && !userlist.isEmpty()) {
User user = (User) userlist.get(0);
//check double
List<GitProxy> repos = proxydao.findByProxyname(proxyname);
if (repos.isEmpty()){
GitProxy gitproxy = new GitProxy();
gitproxy.setProxyname(proxyname);
gitproxy.setUser(user);
proxydao.save(gitproxy);
res = new String[]{"0","OK"};
} else {
logger.error("Error- This git-proxy already exists");
res = new String[]{"1","Error- This git-proxy already exists"};
}
} else {
logger.error("User with userInstanceUriId: "+userInstanceUriId+" does not exist");
res = new String[]{"1","Error-User with userInstanceUriId: "+userInstanceUriId+" does not exist"};
}
} else {
logger.error("SpringBean userdao cannot be instantiated");
res = new String[]{"1","Error-Internal Server - Spring Bean cannot be initialized"};
}
return res;
}
public String[] bindProxyToGit(String userInstanceUriId, String proxyid, String gitid ) {
String[] res = {"1","Internal Server Error"};
if (userdao != null) {
List userlist = userdao.findBy("uriID", userInstanceUriId);
if (userlist != null && !userlist.isEmpty()) {
User user = (User) userlist.get(0);
try { //TODO add findby
//GitRepo
List<GitRepo> repos = repodao.findByUserAndGitrepoid(user, new Long(gitid));
//GitProxy
List<GitProxy> proxies = proxydao.findByUserAndGitproxyid(user,new Long(proxyid));
if (!repos.isEmpty()){
if (!proxies.isEmpty()){
GitRepo repo = repos.get(0);
GitProxy proxy = proxies.get(0);
if (repo!=null && proxy!=null) {
//delete old from file
Util.replaceBlockWithSedForProxyGit(PROXY_GIT_FILE, ""+proxy.getId());
//change the database
proxy.setRepo(repo);
proxydao.save(proxy);
//create new file
String segment = Util.createProxyGitSegment( ""+proxy.getId() , proxy.getProxyname() , repo.getGiturl() , repo.getGitrepo() , user.getId() );
logger.info("writing to PROXY_GIT_FILE:|"+PROXY_GIT_FILE+"|");
Util.appendToFile(PROXY_GIT_FILE,segment);
logger.info("Success-Repo created" );
//return
String gitcommand = "git remote add origin "+SERVER_ACCOUNT_NAME+"@"+SERVER_IP_ADDRESS+":"+proxy.getProxyname();
res = new String[]{"0","OK - You should execute "+gitcommand};
} else {
throw new Exception("Repo or Proxy does not exist");
}
} else {
logger.error("Proxy with this id does not exist for the specific user");
res = new String[]{"1","Proxy with this id does not exist for the specific user"};
}
} else {
logger.error("Repository with this id does not exist for the specific user");
res = new String[]{"1","Repository with this id does not exist for the specific user"};
}
} catch(Exception ex){
logger.error("Git Or Proxy with this id does not exist");
res = new String[]{"1","Error-Git Or Proxy with this id does not exist"};
}
} else {
logger.error("User with userInstanceUriId: "+userInstanceUriId+" does not exist");
res = new String[]{"1","Error-User with userInstanceUriId: "+userInstanceUriId+" does not exist"};
}
} else {
logger.error("SpringBean userdao cannot be instantiated");
res = new String[]{"1","Error-Internal Server - Spring Bean cannot be initialized"};
}
return res;
}
public GitRepoInfo getGitProxyInfos(String userInstanceUriId, String paaSInstanceUriId, String applicationInstanceUriId) throws SOAException{
Paas paas = null;
ApplicationInstance app = null;
if (userdao != null) {
List userlist = userdao.findBy("uriID", userInstanceUriId);
if (userlist != null && !userlist.isEmpty()) {
User user = (User) userlist.get(0);
logger.info("userid:"+user.getId());
List paaslist = paasdao.findBy("uriID", paaSInstanceUriId);
if (paaslist != null && !paaslist.isEmpty()) {
paas = (Paas) paaslist.get(0);
List accountlist= accountdao.retrieve(user.getId(), paas.getId());
Account account = (Account) accountlist.get(0);
List applist = appdao.findByUriIDAndAccountId(applicationInstanceUriId,account.getId());
if (applist != null && !applist.isEmpty()) {
app = (ApplicationInstance) applist.get(0);
logger.info("Retrieved applicationInstanceUriId: " + applicationInstanceUriId + " app:" + app.getId());
} else {
logger.error("App with applicationInstanceUriId: " + applicationInstanceUriId + " does not exist");
throw new SOAException(Response.Status.INTERNAL_SERVER_ERROR, "Error-App with applicationInstanceUriId: " + applicationInstanceUriId + " does not exist");
}
logger.info("Retrieved paaSInstanceUriId: " + paaSInstanceUriId + " paasId:" + paas.getId());
} else {
logger.error("PaaS with paaSInstanceUriId: " + paaSInstanceUriId + " does not exist");
throw new SOAException(Response.Status.INTERNAL_SERVER_ERROR, "Error-PaaS with paaSInstanceUriId: " + paaSInstanceUriId + " does not exist");
}
//check double
List<GitRepo> repos = repodao.findByAppAndUserAndPaaS(app, user, paas);
if (!repos.isEmpty()) {
GitRepo repo = (GitRepo) repos.get(0);
GitRepo gitrepo = new GitRepo();
gitrepo.setGiturl(repo.getGiturl());
gitrepo.setGitrepo(repo.getGitrepo());
//gitrepo.setUser(user);
gitrepo.setUser(user);
//gitrepo.setPaas(paas);
gitrepo.setPaas(paas);
gitrepo.setApp(app);
repodao.save(gitrepo);
logger.info("Success-Repo registered");
GitRepoInfo gitRepoInfo = new GitRepoInfo();
gitRepoInfo.setUrl(repo.getGiturl());
gitRepoInfo.setRepositoryName(repo.getGitrepo());
gitRepoInfo.setApplicationId("" + app.getId());
gitRepoInfo.setUserId("" + user.getId());
gitRepoInfo.setApplicationUrl(app.getAppurl());
gitRepoInfo.setAdapterUrl(app.getAdapterurl());
return gitRepoInfo;
} else {
logger.error("App with applicationInstanceUriId: " + applicationInstanceUriId + " does not exist");
throw new SOAException(Response.Status.INTERNAL_SERVER_ERROR, "Error-App with applicationInstanceUriId: " + applicationInstanceUriId + " does not exist");
}
} else {
logger.error("User with userInstanceUriId: " + userInstanceUriId + " does not exist");
throw new SOAException(Response.Status.INTERNAL_SERVER_ERROR, "Error-User with userInstanceUriId: " + userInstanceUriId + " does not exist");
}
} else {
logger.error("SpringBean userdao cannot be instantiated");
throw new SOAException(Response.Status.INTERNAL_SERVER_ERROR, "Error-Internal Server - Spring Bean cannot be initialized");
}
}
public List<GitRepo> getGitRepositoriesForUser(String userInstanceUriId) throws SOAException {
if (userdao != null) {
List userlist = userdao.findBy("uriID", userInstanceUriId);
if (userlist != null && !userlist.isEmpty()) {
User user = (User) userlist.get(0);
List<GitRepo> repos = repodao.findByUser(user);
logger.info("Success-User with userInstanceUriId: " + userInstanceUriId + " has "+repos.size()+" repos");
return repos;
} else {
logger.error("User with userInstanceUriId: "+userInstanceUriId+" does not exist");
throw new SOAException(Response.Status.BAD_REQUEST, "Error-User with userInstanceUriId: "+userInstanceUriId+" does not exist");
}
} else {
logger.error("SpringBean userdao cannot be instantiated");
throw new SOAException(Response.Status.INTERNAL_SERVER_ERROR, "Error-Internal Server - Spring Bean cannot be initialized");
}
}
public List<GitProxy> getGitProxiesForUser(String userInstanceUriId) throws SOAException {
String[] res = {"1","Internal Server Error"};
if (userdao != null) {
List userlist = userdao.findBy("uriID", userInstanceUriId);
if (userlist != null && !userlist.isEmpty()) {
User user = (User) userlist.get(0);
List<GitProxy> proxies = proxydao.findByUser(user);
logger.info("Success-User with userInstanceUriId: " + userInstanceUriId + " has "+proxies.size()+" proxies");
return proxies;
} else {
logger.error("User with userInstanceUriId: "+userInstanceUriId+" does not exist");
throw new SOAException(Response.Status.BAD_REQUEST, "Error-User with userInstanceUriId: "+userInstanceUriId+" does not exist");
}
} else {
logger.error("SpringBean userdao cannot be instantiated");
throw new SOAException(Response.Status.INTERNAL_SERVER_ERROR, "Error-Internal Server - Spring Bean cannot be initialized");
}
}
public String[] deleteGitRepositoryFromUserByRepoid(String userInstanceUriId, String repoid) {
String[] res = {"1","Internal Server Error"};
if (userdao != null) {
List userlist = userdao.findBy("uriID", userInstanceUriId);
if (userlist != null && !userlist.isEmpty()) {
User user = (User) userlist.get(0);
try{
List<GitRepo> repos = repodao.findByUserAndGitrepoid(user, new Long(repoid));
if (!repos.isEmpty()){
//delete them from the database
repodao.delete(repos);
logger.info("Success-Deleted repository for user with userInstanceUriId: " + userInstanceUriId );
res = new String[]{"0","OK"};
} else {
logger.error("User with userInstanceUriId: "+userInstanceUriId+" does not contain the specific repository");
res = new String[]{"1","User with userInstanceUriId: "+userInstanceUriId+" does not contain the specific repository"};
}
} catch (Exception ex) {
logger.error("Error-Internal Server Error "+ex);
res = new String[]{"1","Error-Internal Server Error "+ex};
}
} else {
logger.error("User with userInstanceUriId: "+userInstanceUriId+" does not exist");
res = new String[]{"1","Error-User with userInstanceUriId: "+userInstanceUriId+" does not exist"};
}
} else {
logger.error("SpringBean userdao cannot be instantiated");
res = new String[]{"1","Error-Internal Server - Spring Bean cannot be initialized"};
}
return res;
}
public String[] deleteGitProxyFromUserByProxyid(String userInstanceUriId, String proxyid) {
String[] res = {"1","Internal Server Error"};
if (userdao != null) {
List userlist = userdao.findBy("uriID", userInstanceUriId);
if (userlist != null && !userlist.isEmpty()) {
User user = (User) userlist.get(0);
try{
List<GitProxy> proxies = proxydao.findByUserAndGitproxyid(user, new Long(proxyid));
if (!proxies.isEmpty()){
//delete them from file
for (int i = 0; i < proxies.size(); i++) {
GitProxy proxy = proxies.get(i);
Util.replaceBlockWithSedForProxyGit(PROXY_GIT_FILE, proxy.getId()+"");
//delete them from the database
proxydao.delete(proxy);
}
logger.info("Success-Deleted proxyname for user with userInstanceUriId: " + userInstanceUriId );
res = new String[]{"0","OK"};
} else {
logger.error("User with userInstanceUriId: "+userInstanceUriId+" does not contain the specific proxy");
res = new String[]{"1","User with userInstanceUriId: "+userInstanceUriId+" does not contain the specific proxy"};
}
} catch (Exception ex) {
logger.error("Error-Internal Server Error "+ex);
res = new String[]{"1","Error-Internal Server Error "+ex};
}
} else {
logger.error("User with userInstanceUriId: "+userInstanceUriId+" does not exist");
res = new String[]{"1","Error-User with userInstanceUriId: "+userInstanceUriId+" does not exist"};
}
} else {
logger.error("SpringBean userdao cannot be instantiated");
res = new String[]{"1","Error-Internal Server - Spring Bean cannot be initialized"};
}
return res;
}
// @WebMethod
// public String[] updateGitRepositoryFromUserByProxyname(String username, String password, String proxyname, String giturl , String reponame) {
// String[] res = {"1","Internal Server Error"};
// if (userdao != null) {
// List userlist = userdao.findByUsernameAndPassword(username, password);
// if (userlist != null && !userlist.isEmpty()) {
// User user = (User) userlist.get(0);
// try{
// List<GitRepo> repos = repodao.findByUserAndProxyname(user, proxyname);
// if (!repos.isEmpty()){
// //delete old from file
// Util.replaceBlockWithSedForProxyGit(PROXY_GIT_FILE, proxyname);
// //replace db entry
// for (int i = 0; i < repos.size(); i++) {
// GitRepo gitrepo = repos.get(i);
// gitrepo.setGiturl(giturl);
// gitrepo.setGitrepo(reponame);
// repodao.save(gitrepo);
// }
// //create new file segment
// String segment = Util.createProxyGitSegment(proxyname,giturl,reponame);
// Util.appendToFile(PROXY_GIT_FILE,segment);
// logger.info("Success-Updated proxyname for user with username: " + username );
// res = new String[]{"0","OK"};
// } else {
// logger.error("User with username: "+username+" does not contain the specific repository");
// res = new String[]{"1","User with username: "+username+" does not contain the specific repository"};
// }
// } catch (Exception ex) {
// logger.error("Error-Internal Server Error "+ex);
// res = new String[]{"1","Error-Internal Server Error "+ex};
// }
// } else {
// logger.error("User with username: "+username+" password: "+password+" does not exist");
// res = new String[]{"1","Error-User with username: "+username+" password: "+password+" does not exist"};
// }
// } else {
// logger.error("SpringBean userdao cannot be instantiated");
// res = new String[]{"1","Error-Internal Server - Spring Bean cannot be initialized"};
// }
// return res;
// }
@Override
public String toString() {
return "Properties: "+
"AUTHORIZED_KEYS_FILE:|"+AUTHORIZED_KEYS_FILE+"| "+
"C4SOA_SERVER_PUBLIC_KEY:|"+C4SOA_SERVER_PUBLIC_KEY+"| "+
"PROXY_GIT_FILE:|"+PROXY_GIT_FILE+"| "+
"SERVER_ACCOUNT_NAME:|"+SERVER_ACCOUNT_NAME+"| "+
"SERVER_IP_ADDRESS:|"+SERVER_IP_ADDRESS+"|";
}
public String getProxyId(String proxyName) throws SOAException {
String proxyid = "";
List<GitProxy> proxies = proxydao.findByProxyname(proxyName);
if (proxies != null && !proxies.isEmpty()) {
proxyid = "" + ((GitProxy) proxies.get(0)).getId();
} else {
String message = "Proxy with proxyName: " + proxyName + " does not exist";
logger.error(message);
throw new SOAException(Response.Status.INTERNAL_SERVER_ERROR, message);
}
return proxyid;
}
public String getRepoId(String repoName , String gitUrl) throws SOAException {
String gitid = "";
List<GitRepo> repos = repodao.findByGitrepoAndGitUrl(repoName, gitUrl);
if (repos != null && !repos.isEmpty()) {
gitid = "" + ((GitRepo) repos.get(0)).getId();
} else {
String message = "Repository with repoName: " + repoName + " does not exist";
logger.error(message);
throw new SOAException(Response.Status.INTERNAL_SERVER_ERROR, message);
}
return gitid;
}
public String getApplicationUrl(String applicationInstanceUriId, String paaSInstanceUriId) throws SOAException {
String appUrl;
//1st list needed to find the proper user id( calling findByUriIdNoCheck because we don't care about multiple instances)
List<ApplicationInstance> firstList = appdao.findByUriIdNoCheck(applicationInstanceUriId);
if (firstList != null && !firstList.isEmpty()) {
User user =firstList.get(0).getAccount().getUser();
//in case of simple deployemnt
if (firstList.size()==1){
appUrl = "" + ((ApplicationInstance) firstList.get(0)).getAppurl();
return appUrl;
}
//In case of migration we have 2 applciations with same applicationInstanceUriId and different AccountId
//then get the application list by applicationInstanceUriId and Account Id
List paaslist = paasdao.findBy("uriID", paaSInstanceUriId);
if (paaslist != null && !paaslist.isEmpty()) {
Paas paas = (Paas) paaslist.get(0);
List accountlist= accountdao.retrieve(user.getId(), paas.getId());
Account account = (Account) accountlist.get(0);
List<ApplicationInstance> appList = appdao.findByUriIDAndAccountId(applicationInstanceUriId,account.getId());
// List<ApplicationInstance> appList = appdao.findByUriId(applicationInstanceUriId);
if (appList != null && !appList.isEmpty()) {
appUrl = "" + ((ApplicationInstance) appList.get(0)).getAppurl();
} else {
String message = "Application with applicationInstanceUriId: " + applicationInstanceUriId + " does not exist for the account "+account.getId();
logger.error(message);
throw new SOAException(Response.Status.INTERNAL_SERVER_ERROR, message);
}
}else {
String message = "Paas with paaSInstanceUriId: " + paaSInstanceUriId + " was not found";
logger.error(message);
throw new SOAException(Response.Status.INTERNAL_SERVER_ERROR, message);
}
}else {
String message = "Application with abblicationInstanceUriId: " + applicationInstanceUriId + " does not exist";
logger.error(message);
throw new SOAException(Response.Status.INTERNAL_SERVER_ERROR, message);
}
return appUrl;
}
} //EoC