use of ca.nrc.cadc.caom2.ObservationState in project caom2db by opencadc.
the class ObservationStateListReader method read.
/**
* Read and parse content into ObservationState(s).
*
* @param in
* @return list of ObservationState
* @throws IOException failed to read
* @throws IllegalArgumentException invalid mandatory content (collection observationID)
* @throws ParseException invalid maxlastModified timestamp
* @throws URISyntaxException invalid accMetaChecksum URI
*/
@Override
public List<ObservationState> read(Reader in) throws IOException, ParseException, URISyntaxException {
final List<ObservationState> ret = new ArrayList<>(1000);
LineNumberReader reader = new LineNumberReader(in, 16384);
String line = reader.readLine();
while (line != null) {
try {
line = line.trim();
if (!line.isEmpty()) {
// <Observation.collection> <Observation.observationID> <Observation.maxLastModified> <Observation.accMetaChecksum>
// ICD says tabs but be generous and split of any whitespace
String[] tokens = line.split("[ \t]");
String collection = tokens[0];
String observationID = tokens[1];
ObservationURI uri = new ObservationURI(collection, observationID);
ObservationState os = new ObservationState(uri);
//
if (tokens.length > 2 && tokens[2].length() > 0) {
os.maxLastModified = DateUtil.flexToDate(tokens[2], dateFormat);
}
if (tokens.length > 3 && tokens[3].length() > 0) {
os.accMetaChecksum = new URI(tokens[3]);
}
ret.add(os);
}
line = reader.readLine();
} catch (Exception ex) {
log.error("read failure on line " + reader.getLineNumber(), ex);
throw ex;
} finally {
if (reader.getLineNumber() % 100 == 0) {
log.debug("read: line " + reader.getLineNumber());
}
}
}
return ret;
}
use of ca.nrc.cadc.caom2.ObservationState in project caom2db by opencadc.
the class PutAction method doAction.
@Override
public void doAction() throws Exception {
ObservationURI uri = getURI();
log.debug("START: " + uri);
checkWritePermission();
Observation obs = getInputObservation();
if (!uri.equals(obs.getURI())) {
throw new IllegalArgumentException("invalid input: " + uri + " (path) must match : " + obs.getURI() + "(document)");
}
ObservationDAO dao = getDAO();
ObservationState s = dao.getState(obs.getID());
if (s != null) {
throw new ResourceAlreadyExistsException("already exists: " + uri);
}
validate(obs);
dao.put(obs);
log.debug("DONE: " + uri);
}
use of ca.nrc.cadc.caom2.ObservationState in project caom2db by opencadc.
the class AbstractObservationDAOTest method testGetDeleteNonExistentObservation.
@Test
public void testGetDeleteNonExistentObservation() {
try {
ObservationURI uri = new ObservationURI("TEST", "NonExistentObservation");
Observation obs = dao.get(uri);
Assert.assertNull(uri.toString(), obs);
ObservationState notFound = dao.getState(uri);
Assert.assertNull(uri.toString(), notFound);
UUID uuid = UUID.randomUUID();
ObservationState nuri = dao.getState(uuid);
Assert.assertNull(uuid.toString(), nuri);
Observation nobs = dao.get(uuid);
Assert.assertNull(uuid.toString(), nobs);
// should return without failing
dao.delete(uuid);
} catch (Exception unexpected) {
log.error("unexpected exception", unexpected);
Assert.fail("unexpected exception: " + unexpected);
}
}
use of ca.nrc.cadc.caom2.ObservationState in project caom2db by opencadc.
the class RepoClient method readDeletedEntityList.
private List<DeletedObservation> readDeletedEntityList(DeletionListReader transformer, String collection, Date start, Date end, Integer maxrec) {
List<DeletedObservation> accList = new ArrayList<>();
List<DeletedObservation> partialList = null;
boolean tooBigRequest = maxrec == null || maxrec > DEFAULT_BATCH_SIZE;
Integer rec = maxrec;
Integer recCounter;
if (tooBigRequest) {
rec = DEFAULT_BATCH_SIZE;
}
// Use HttpDownload to make the http GET calls (because it handles a lot
// of the
// authentication stuff)
boolean go = true;
String surlCommon = baseDeletionURL.toExternalForm() + File.separator + collection;
while (go) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
if (!tooBigRequest) {
// only one go
go = false;
}
String surl = surlCommon;
surl = surl + "?maxRec=" + (rec + 1);
if (start != null) {
surl = surl + "&start=" + df.format(start);
}
if (end != null) {
surl = surl + "&end=" + df.format(end);
}
URL url;
log.debug("URL: " + surl);
try {
url = new URL(surl);
HttpDownload get = new HttpDownload(url, bos);
get.setFollowRedirects(true);
get.run();
int responseCode = get.getResponseCode();
log.debug("RESPONSE CODE: '" + responseCode + "'");
if (responseCode == 302) {
// redirected url
url = get.getRedirectURL();
log.debug("REDIRECTED URL: " + url);
bos = new ByteArrayOutputStream();
get = new HttpDownload(url, bos);
responseCode = get.getResponseCode();
log.debug("RESPONSE CODE (REDIRECTED URL): '" + responseCode + "'");
}
if (get.getThrowable() != null) {
if (get.getThrowable() instanceof AccessControlException) {
throw (AccessControlException) get.getThrowable();
}
throw new RuntimeException("failed to get observation list", get.getThrowable());
}
} catch (MalformedURLException e) {
throw new RuntimeException("BUG: failed to generate observation list url", e);
}
try {
// log.debug("RESPONSE = '" + bos.toString() + "'");
partialList = transformer.read(new ByteArrayInputStream(bos.toByteArray()));
// df, '\t', '\n');
if (partialList != null && !partialList.isEmpty() && !accList.isEmpty() && accList.get(accList.size() - 1).equals(partialList.get(0))) {
partialList.remove(0);
}
if (partialList != null) {
accList.addAll(partialList);
log.debug("adding " + partialList.size() + " elements to accList. Now there are " + accList.size());
}
bos.close();
} catch (ParseException | URISyntaxException | IOException e) {
throw new RuntimeException("Unable to list of ObservationState from " + bos.toString(), e);
}
if (accList.size() > 0) {
start = accList.get(accList.size() - 1).getLastModified();
}
recCounter = accList.size();
if (maxrec != null && maxrec - recCounter > 0 && maxrec - recCounter < rec) {
rec = maxrec - recCounter;
}
int i = 0;
for (DeletedObservation de : accList) {
log.debug("accList.get( " + i++ + ") = " + de.getLastModified());
}
log.debug("accList.size() = " + accList.size());
log.debug("dynamic batch (rec): " + rec);
log.debug("maxrec: " + maxrec);
log.debug("start: " + start);
log.debug("end: " + end);
if (partialList != null) {
log.debug("partialList.size(): " + partialList.size());
if (partialList.size() < rec || (end != null && start != null && start.equals(end))) {
log.debug("************** go false");
go = false;
}
}
}
return partialList;
}
use of ca.nrc.cadc.caom2.ObservationState in project caom2db by opencadc.
the class RepoClient method get.
public ObservationResponse get(ObservationURI uri) {
if (uri == null) {
throw new IllegalArgumentException("uri cannot be null");
}
ObservationState os = new ObservationState(uri);
// see comment above in getList
Subject subjectForWorkerThread = AuthenticationUtil.getCurrentSubject();
Worker wt = new Worker(os, subjectForWorkerThread, baseServiceURL.toExternalForm());
return wt.getObservation();
}
Aggregations