Commit cd105853 authored by Michael Ritter's avatar Michael Ritter
Browse files

#27 Pull PeerCollections from the rest api

parent 0113df81
......@@ -31,14 +31,19 @@
package edu.umiacs.ace.monitor.compare;
import edu.umiacs.ace.monitor.access.CollectionCountContext;
import edu.umiacs.ace.util.EntityManagerServlet;
import edu.umiacs.ace.remote.JsonGateway;
import edu.umiacs.ace.monitor.peers.PartnerSite;
import edu.umiacs.ace.monitor.core.MonitoredItem;
import edu.umiacs.ace.monitor.core.Collection;
import edu.umiacs.ace.monitor.core.MonitoredItem;
import edu.umiacs.ace.monitor.peers.PartnerSite;
import edu.umiacs.ace.remote.JsonGateway;
import edu.umiacs.ace.util.EntityManagerServlet;
import edu.umiacs.util.Strings;
import java.io.IOException;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;
import org.apache.log4j.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.servlet.RequestDispatcher;
......@@ -46,15 +51,9 @@ import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;
import org.apache.log4j.Logger;
import java.io.IOException;
/**
*
* @author toaster
*/
public class CompareServlet extends EntityManagerServlet {
......@@ -67,14 +66,15 @@ public class CompareServlet extends EntityManagerServlet {
public static final String PARAM_REMOTRE_COLLECTIONID = "remotecollectionid";
public static final String PARAM_SOURCE = "source";
/**
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
*
* @param request servlet request
* @param response servlet response
*/
@Override
protected void processRequest( HttpServletRequest request,
HttpServletResponse response, EntityManager em )
protected void processRequest(HttpServletRequest request,
HttpServletResponse response, EntityManager em)
throws ServletException, IOException {
Collection c = null;
MonitoredItem monItem = null;
......@@ -86,7 +86,7 @@ public class CompareServlet extends EntityManagerServlet {
long remoteCollection = 0;
if ( !ServletFileUpload.isMultipartContent(request) ) {
if (!ServletFileUpload.isMultipartContent(request)) {
throw new ServletException("No file is attached");
}
......@@ -96,68 +96,68 @@ public class CompareServlet extends EntityManagerServlet {
// Parse the request
try {
FileItemIterator iter = upload.getItemIterator(request);
while ( iter.hasNext() ) {
while (iter.hasNext()) {
FileItemStream item = iter.next();
if ( item.isFormField() ) {
if ( PARAM_COLLECTION_ID.equals(item.getFieldName()) ) {
if (item.isFormField()) {
if (PARAM_COLLECTION_ID.equals(item.getFieldName())) {
String col = Streams.asString(item.openStream());
if ( Strings.isValidLong(col) ) {
if (Strings.isValidLong(col)) {
long colId = Long.parseLong(col);
c = em.getReference(Collection.class, colId);
if ( c == null ) {
if (c == null) {
throw new ServletException(
"Collection does not exist: " + colId);
}
}
} else if ( PARAM_ITEM_ID.equals(item.getFieldName()) ) {
} else if (PARAM_ITEM_ID.equals(item.getFieldName())) {
String itemString = Streams.asString(item.openStream());
long itemId = Long.parseLong(itemString);
try {
monItem = em.getReference(MonitoredItem.class,
itemId);
} catch ( EntityNotFoundException e ) {
} catch (EntityNotFoundException e) {
monItem = null;
}
} else if ( PARAM_REMOTESITEID.equals(item.getFieldName()) ) {
} else if (PARAM_REMOTESITEID.equals(item.getFieldName())) {
String siteString = Streams.asString(item.openStream());
if ( Strings.isValidLong(siteString) ) {
if (Strings.isValidLong(siteString)) {
long partnerId = Long.parseLong(siteString);
try {
partner = em.getReference(PartnerSite.class,
partnerId);
} catch ( EntityNotFoundException e ) {
} catch (EntityNotFoundException e) {
partner = null;
}
}
} else if ( PARAM_REMOTRE_COLLECTIONID.equals(
item.getFieldName()) ) {
} else if (PARAM_REMOTRE_COLLECTIONID.equals(
item.getFieldName())) {
String collString = Streams.asString(item.openStream());
if ( Strings.isValidLong(collString) ) {
if (Strings.isValidLong(collString)) {
remoteCollection = Long.parseLong(collString);
}
} else if ( PARAM_FILTER.equals(item.getFieldName()) ) {
} else if (PARAM_FILTER.equals(item.getFieldName())) {
inputFilter = Streams.asString(item.openStream());
if ( Strings.isEmpty(inputFilter) ) {
if (Strings.isEmpty(inputFilter)) {
inputFilter = null;
}
} else if ( PARAM_SOURCE.equals(item.getFieldName()) ) {
} else if (PARAM_SOURCE.equals(item.getFieldName())) {
String isAttached = Streams.asString(item.openStream());
fileAttached = "upload".equals(isAttached);
}
}
if ( !item.isFormField() && fileAttached ) {
if (!item.isFormField() && fileAttached) {
LOG.debug(
"item " + monItem + " filter " + inputFilter + " loading attached file");
CollectionCompare2 cc = new CollectionCompare2(
item.openStream(), inputFilter);
CompareResults cr = new CompareResults(cc);
Thread t = new Thread(new TableCompareRunnable(cr,cc, c,
Thread t = new Thread(new TableCompareRunnable(cr, cc, c,
monItem), "Compare Thread " + c.getName());
t.start();
session.setAttribute(PAGE_RESULTS, cr);
......@@ -166,21 +166,21 @@ public class CompareServlet extends EntityManagerServlet {
LOG.debug(
"fileattached: " + fileAttached + " partner " + partner + " remote coll: "
+ remoteCollection);
+ remoteCollection);
// we have no attached file, load remote
if ( !fileAttached && partner != null && remoteCollection > 0 ) {
if (!fileAttached && partner != null && remoteCollection > 0) {
LOG.debug("Remote digest request " + partner.getRemoteURL());
CollectionCompare2 cc = new CollectionCompare2(
JsonGateway.getGateway().getDigestList(partner,
remoteCollection), inputFilter);
CompareResults cr = new CompareResults(cc);
remoteCollection), inputFilter);
CompareResults cr = new CompareResults(cc);
Thread t = new Thread(new TableCompareRunnable(cr,cc, c,
Thread t = new Thread(new TableCompareRunnable(cr, cc, c,
monItem), "Compare Thread " + c.getName());
t.start();
session.setAttribute(PAGE_RESULTS, cr);
}
} catch ( FileUploadException ful ) {
} catch (FileUploadException ful) {
throw new ServletException(ful);
}
......@@ -196,8 +196,8 @@ public class CompareServlet extends EntityManagerServlet {
private MonitoredItem baseItem;
private CompareResults cr;
private TableCompareRunnable( CompareResults cr,CollectionCompare2 cc, Collection c,
MonitoredItem baseItem ) {
private TableCompareRunnable(CompareResults cr, CollectionCompare2 cc, Collection c,
MonitoredItem baseItem) {
this.cr = cr;
this.cc = cc;
this.c = c;
......@@ -206,15 +206,7 @@ public class CompareServlet extends EntityManagerServlet {
@Override
public void run() {
try {
cc.compareTo(cr,c, baseItem);
// cc.loadCollectionTable(c, baseItem);
// cc.doCompare();
// cc.getUnseenTargetFiles();
// cc.getUnseenSuppliedFiles();
} finally {
// cc.cleanup();
}
cc.compareTo(cr, c, baseItem);
}
}
}
......@@ -31,6 +31,7 @@
package edu.umiacs.ace.monitor.peers;
import edu.umiacs.ace.monitor.core.Collection;
import edu.umiacs.ace.remote.JsonGateway;
import edu.umiacs.ace.remote.PeerAuthenticator;
import edu.umiacs.ace.remote.StatusBean.CollectionBean;
......@@ -105,6 +106,10 @@ public class PartnerSiteContextListener implements ServletContextListener {
return JsonGateway.getGateway().getStatusBean(site).getCollections();
}
public List<Collection> getCollectionsRest() {
return JsonGateway.getGateway().getCollectionListRest(site);
}
@Override
public Long getId() {
return site.getId();
......
......@@ -33,19 +33,6 @@ package edu.umiacs.ace.monitor.peers;
import edu.umiacs.ace.util.EntityManagerServlet;
import edu.umiacs.util.Strings;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
......@@ -57,6 +44,17 @@ import org.apache.http.impl.client.HttpClients;
import org.apache.log4j.Logger;
import sun.misc.BASE64Encoder;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
/**
* Manage partner sites
* partnerid = db id of partner site
......@@ -126,8 +124,7 @@ public final class PartnerSiteServlet extends EntityManagerServlet {
et.begin();
em.persist(partner);
et.commit();
} else if ( !Strings.isEmpty(user) || !Strings.isEmpty(pass) || !Strings.isEmpty(
url) ) {
} else if (!Strings.isEmpty(user) || !Strings.isEmpty(pass) || !Strings.isEmpty(url)) {
request.setAttribute(PAGE_ERROR, outcome);
}
......
......@@ -33,6 +33,7 @@ package edu.umiacs.ace.remote;
import com.fasterxml.jackson.databind.DeserializationConfig;
import com.fasterxml.jackson.databind.ObjectMapper;
import edu.umiacs.ace.monitor.core.Collection;
import edu.umiacs.ace.monitor.peers.PartnerSite;
import edu.umiacs.util.Strings;
import org.apache.http.HttpResponse;
......@@ -50,7 +51,9 @@ import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
......@@ -71,8 +74,10 @@ public class JsonGateway {
private static String ITEM_SUFFIX = "/ListItem?json=1&collectionid=";
private static String ITEMROOT_SUFFIX = "/ListItem?json=1&collectionid=";
private static String DIGEST_SUFFIX = "/Summary?collectionid=";
private static String GROUP_LIST_SUFFIX = "/rest/groups";
private static String COLL_LIST_SUFFIX = "/rest/collections";
private final ObjectMapper mapper;
private Map<PartnerSite, JsonCache> cache = new HashMap();
private Map<PartnerSite, JsonCache> cache = new HashMap<>();
private JsonGateway() {
mapper = new ObjectMapper();
......@@ -258,18 +263,28 @@ public class JsonGateway {
}
}
public List<Collection> getCollectionListRest(PartnerSite site) {
try {
URL u = new URL(site.getRemoteURL() + COLL_LIST_SUFFIX);
return readJSONValue(u, site.getCredentials(), List.class);
} catch (IOException e) {
LOG.error("Error reading site " + site.getRemoteURL(), e);
return Collections.emptyList();
}
}
private static class JsonCache {
private StatusBean status = null;
private long statusUpdate = 0;
private SummaryBean summary = null;
private long summaryUpdate = 0;
private Map<Long, ReportBean> reportMap = new HashMap<Long, ReportBean>();
private Map<Long, Long> reportMapUpdate = new HashMap<Long, Long>();
private Map<String, ParentChildBean> itemMap = new HashMap<String, ParentChildBean>();
private Map<String, Long> itemMapUpdate = new HashMap<String, Long>();
private Map<Long, ParentChildBean> itemRootMap = new HashMap<Long, ParentChildBean>();
private Map<Long, Long> itemRootMapUpdate = new HashMap<Long, Long>();
private Map<Long, ReportBean> reportMap = new HashMap<>();
private Map<Long, Long> reportMapUpdate = new HashMap<>();
private Map<String, ParentChildBean> itemMap = new HashMap<>();
private Map<String, Long> itemMapUpdate = new HashMap<>();
private Map<Long, ParentChildBean> itemRootMap = new HashMap<>();
private Map<Long, Long> itemRootMapUpdate = new HashMap<>();
}
private <T> T readJSONValue( URL u, String auth, Class<T> clazz ) throws IOException {
......
<%@page pageEncoding="UTF-8"%>
<%@page pageEncoding="UTF-8" %>
<%--
The taglib directive below imports the JSTL library. If you uncomment it,
you must also add the JSTL library to the project. The Add Library... action
on Libraries node in Projects view can be used to add the JSTL 1.1 library.
--%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<c:if test="${workingCollection == null}">
<c:redirect url="Status"/>
......@@ -16,140 +15,137 @@ PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Compare Collection</title>
<link rel="stylesheet" type="text/css" href="style.css" />
<style>
#compareOr {
text-align: center;
margin-top: 15px;
margin-bottom: 15px;
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Compare Collection</title>
<link rel="stylesheet" type="text/css" href="style.css"/>
<style>
#compareOr {
text-align: center;
margin-top: 15px;
margin-bottom: 15px;
}
#compareInputFile {
margin-top: 15px;
}
#compareSubmitButton {
margin-top: 15px;
}
#compareToSite {
margin-top: 15px;
display: none;
}
#collectionSelection {
display: none;
}
</style>
<script type="text/javascript">
var collectionArray = new Array();
<c:forEach var="partner" items="${partnerList.sites}" varStatus="pstatus">
var partnerArray = [];
collectionArray[${pstatus.index + 1}] = partnerArray;
<c:forEach var="collection" items="${partner.collectionsRest}" varStatus="cstatus">
partnerArray[${cstatus.index}] = {
'id':${collection.id},
'name': '${collection.group} ${collection.name}',
'shortname': '${collection.name}'
};
</c:forEach>
</c:forEach>
function setOptions(site) {
var elem = document.getElementById('collectionSelection');
document.compareform.remotecollectionid.length = 0;
if (site == 0) {
document.compareform.remotecollectionid.disabled = true;
elem.style.display = "none";
}
#compareInputFile {
margin-top: 15px;
else if (site == (document.compareform.partnerid.length - 1)) {
window.location = "PartnerSite";
}
#compareSubmitButton {
margin-top: 15px;
else {
document.compareform.remotecollectionid.disabled = false;
var selectedIdx = 0;
for (i = 0; i < collectionArray[site].length; i++) {
var def = false;
if (collectionArray[site][i].shortname == "${workingCollection.collection.name}") {
selectedIdx = i;
def = true
}
document.compareform.remotecollectionid[i] = new Option(collectionArray[site][i].name, collectionArray[site][i].id, def, false);
}
document.compareform.remotecollectionid.selectedIndex = selectedIdx;
elem.style.display = "block";
}
#compareToSite {
margin-top: 15px;
display:none;
}
function toggleVisible() {
var elem;
if (document.compareform.source[0].checked) {
elem = document.getElementById('compareInputFile');
elem.style.display = "block";
elem = document.getElementById('compareToSite');
elem.style.display = "none";
}
#collectionSelection {
display: none;
else {
elem = document.getElementById('compareInputFile');
elem.style.display = "none";
elem = document.getElementById('compareToSite');
elem.style.display = "block";
}
</style>
<script type="text/javascript">
var collectionArray = new Array();
<c:forEach var="partner" items="${partnerList.sites}" varStatus="pstatus">
var partnerArray = [];
collectionArray[${pstatus.index + 1}] = partnerArray;
<c:forEach var="collection" items="${partner.collections}" varStatus="cstatus">
partnerArray[${cstatus.index}] = {'id':${collection.id}, 'name': '${collection.group} ${collection.name}', 'shortname':'${collection.name}'};
}
</script>
</head>
<body>
<jsp:include page="header.jsp"/>
<div class="standardBody">
<h3 class="standardHeader" id="compareCollectionHeader">Report Collection Differences
for ${workingCollection.collection.name}</h3>
<FORM name="compareform" METHOD=POST ENCTYPE="multipart/form-data" ACTION="Compare">
<input type="hidden" name="collectionid" value="${workingCollection.collection.id}">
Compare collection to: <input checked onchange="toggleVisible()" type="radio" name="source"
value="upload">Uploaded File
<input onchange="toggleVisible()" type="radio" name="source" value="partner">Partner
<div id="compareInputFile">
Select file to compare collection against: <INPUT TYPE=FILE NAME="upfile">
</div>
<div id="compareToSite">1. Select Partner Site:
<select name="partnerid" onchange="setOptions(this.selectedIndex)">
<option selected>Select Site</option>
<c:forEach var="item" items="${partnerList.sites}">
<option value="${item.id}">${item.remoteURL}</option>
</c:forEach>
</c:forEach>
function setOptions(site)
{
var elem = document.getElementById('collectionSelection');
document.compareform.remotecollectionid.length=0;
if (site == 0 )
{
document.compareform.remotecollectionid.disabled = true;
elem.style.display = "none";
}
else if (site == (document.compareform.partnerid.length -1))
{
window.location="PartnerSite";
}
else
{
document.compareform.remotecollectionid.disabled = false;
var selectedIdx = 0;
for (i = 0; i < collectionArray[site].length ; i++)
{
var def = false;
if (collectionArray[site][i].shortname == "${workingCollection.collection.name}")
{
selectedIdx = i;
def = true
}
document.compareform.remotecollectionid[i] = new Option(collectionArray[site][i].name, collectionArray[site][i].id,def,false);
}
document.compareform.remotecollectionid.selectedIndex = selectedIdx;
elem.style.display = "block";
}
}
function toggleVisible()
{
var elem;
if ( document.compareform.source[0].checked)
{
elem = document.getElementById('compareInputFile');
elem.style.display = "block";
elem = document.getElementById('compareToSite');
elem.style.display = "none";
}
else
{
elem = document.getElementById('compareInputFile');
elem.style.display = "none";
elem = document.getElementById('compareToSite');
elem.style.display = "block";
}
}
</script>
</head>
<body>
<jsp:include page="header.jsp" />
<div class="standardBody">
<h3 class="standardHeader" id="compareCollectionHeader">Report Collection Differences for ${workingCollection.collection.name}</h3>
<FORM name="compareform" METHOD=POST ENCTYPE="multipart/form-data" ACTION="Compare">
<input type="hidden" name="collectionid" value="${workingCollection.collection.id}">
Compare collection to: <input checked onchange="toggleVisible()" type="radio" name="source" value="upload">Uploaded File
<input onchange="toggleVisible()" type="radio" name="source" value="partner">Partner
<div id="compareInputFile">
Select file to compare collection against: <INPUT TYPE=FILE NAME="upfile">
</div>
<div id="compareToSite">1. Select Partner Site:
<select name="partnerid" onchange="setOptions(this.selectedIndex)">
<option selected>Select Site</option>
<c:forEach var="item" items="${partnerList.sites}">
<option value="${item.id}">${item.remoteURL}</option>
</c:forEach>
<option>Add New</option>
</select>