package com.perforce.spark.site;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.http.HttpResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import spark.Request;
import spark.Response;
import com.perforce.p4java.exception.P4JavaException;
import com.perforce.p4java.server.IOptionsServer;
import com.perforce.spark.ServerError;
import com.perforce.spark.depot.DepotFactory;
import com.perforce.spark.depot.DepotModel;
import com.perforce.spark.repository.RepoFactory;
import com.perforce.spark.repository.RepoFormID;
import com.perforce.spark.repository.RepoInterface;
import com.perforce.spark.store.DefaultStore;
public class GroupSite extends DefaultStore {
private static Logger logger = LoggerFactory.getLogger(GroupSite.class);
private final List<String> list = new ArrayList<>();
public GroupSite(Request request) throws MalformedURLException {
super(request);
String siteStr = RepoFormID.SITE.get(request);
this.site = SiteType.parse(siteStr);
String listStr = RepoFormID.LIST.get(request);
for (String id : listStr.split(",")) {
list.add(id);
}
}
@Override
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("name: " + name + "\n");
sb.append("list: " + list.toString() + "\n");
return sb.toString();
}
@Override
public String getSummaryPattern() {
// nothing stored, so no need to display summary
return null;
}
@Override
public String getPath(Request request) {
type = getArtifactType();
RepoInterface repo = RepoFactory.createStore(type);
return repo.getPath(request);
}
@Override
public HttpServletResponse fetch(IOptionsServer p4, Request request,
Response response) throws Exception {
Exception last = null;
for (String depotName : list) {
DepotModel depot = DepotFactory.getByName(p4, depotName);
RepoInterface repo = depot.getRepo();
try {
HttpServletResponse raw = repo.fetch(p4, request, response);
return raw;
} catch (Exception e) {
last = e;
// try next...
}
}
ServerError error = new ServerError(last);
logger.error(error.getStackTrace());
throw last;
}
@Override
public HttpResponse query(IOptionsServer p4, Request request,
Response response) throws Exception {
return null;
}
public List<DepotModel> getList(IOptionsServer p4) throws P4JavaException {
List<DepotModel> depots = new ArrayList<>();
for (String depotName : list) {
DepotModel depot = DepotFactory.getByName(p4, depotName);
depots.add(depot);
}
return depots;
}
}