Search in sources :

Example 11 with ObservationState

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;
}
Also used : ObservationURI(ca.nrc.cadc.caom2.ObservationURI) ObservationState(ca.nrc.cadc.caom2.ObservationState) ArrayList(java.util.ArrayList) ObservationURI(ca.nrc.cadc.caom2.ObservationURI) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) ParseException(java.text.ParseException) LineNumberReader(java.io.LineNumberReader)

Example 12 with ObservationState

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);
}
Also used : ObservationURI(ca.nrc.cadc.caom2.ObservationURI) ObservationDAO(ca.nrc.cadc.caom2.persistence.ObservationDAO) Observation(ca.nrc.cadc.caom2.Observation) ObservationState(ca.nrc.cadc.caom2.ObservationState) ResourceAlreadyExistsException(ca.nrc.cadc.net.ResourceAlreadyExistsException)

Example 13 with ObservationState

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);
    }
}
Also used : ObservationURI(ca.nrc.cadc.caom2.ObservationURI) DerivedObservation(ca.nrc.cadc.caom2.DerivedObservation) Observation(ca.nrc.cadc.caom2.Observation) DeletedObservation(ca.nrc.cadc.caom2.DeletedObservation) SimpleObservation(ca.nrc.cadc.caom2.SimpleObservation) ObservationState(ca.nrc.cadc.caom2.ObservationState) UUID(java.util.UUID) SQLException(java.sql.SQLException) PreconditionFailedException(ca.nrc.cadc.net.PreconditionFailedException) Test(org.junit.Test)

Example 14 with ObservationState

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;
}
Also used : MalformedURLException(java.net.MalformedURLException) ArrayList(java.util.ArrayList) AccessControlException(java.security.AccessControlException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URL(java.net.URL) HttpDownload(ca.nrc.cadc.net.HttpDownload) ByteArrayInputStream(java.io.ByteArrayInputStream) DeletedObservation(ca.nrc.cadc.caom2.DeletedObservation) ParseException(java.text.ParseException)

Example 15 with ObservationState

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();
}
Also used : ObservationState(ca.nrc.cadc.caom2.ObservationState) Subject(javax.security.auth.Subject)

Aggregations

ObservationState (ca.nrc.cadc.caom2.ObservationState)28 ObservationURI (ca.nrc.cadc.caom2.ObservationURI)11 Observation (ca.nrc.cadc.caom2.Observation)10 ObservationResponse (ca.nrc.cadc.caom2.ObservationResponse)9 DeletedObservation (ca.nrc.cadc.caom2.DeletedObservation)8 SQLException (java.sql.SQLException)8 ArrayList (java.util.ArrayList)8 Test (org.junit.Test)7 PreconditionFailedException (ca.nrc.cadc.net.PreconditionFailedException)6 Date (java.util.Date)6 DerivedObservation (ca.nrc.cadc.caom2.DerivedObservation)5 SimpleObservation (ca.nrc.cadc.caom2.SimpleObservation)5 URISyntaxException (java.net.URISyntaxException)5 ObservationDAO (ca.nrc.cadc.caom2.persistence.ObservationDAO)4 ResourceNotFoundException (ca.nrc.cadc.net.ResourceNotFoundException)4 IOException (java.io.IOException)4 DateFormat (java.text.DateFormat)4 Subject (javax.security.auth.Subject)4 Artifact (ca.nrc.cadc.caom2.Artifact)3 Plane (ca.nrc.cadc.caom2.Plane)3