Search in sources :

Example 1 with SampException

use of org.astrogrid.samp.client.SampException in project aspro by JMMC-OpenDev.

the class VotableSampMessageHandler method processMessage.

/**
 * Implements message processing
 *
 * @param senderId public ID of sender client
 * @param message message with MType this handler is subscribed to
 * @throws SampException if any error occured while message processing
 */
@Override
protected void processMessage(final String senderId, final Message message) throws SampException {
    if (logger.isDebugEnabled()) {
        logger.debug("\tReceived '{}' message from '{}' : '{}'.", this.handledMType(), senderId, message);
    }
    // get url of votable (locally stored) :
    final String voTableURL = (String) message.getRequiredParam("url");
    logger.debug("processMessage: VOTable URL = {}", voTableURL);
    if (voTableURL == null) {
        throw new SampException("Can not get the url of the votable");
    }
    URI voTableURI;
    try {
        voTableURI = new URI(voTableURL);
    } catch (URISyntaxException use) {
        logger.error("invalid URI", use);
        throw new SampException("Can not read the votable : " + voTableURL, use);
    }
    File voTableFile = null;
    try {
        final String scheme = voTableURI.getScheme();
        if (scheme.equalsIgnoreCase("file")) {
            try {
                voTableFile = new File(voTableURI);
            } catch (IllegalArgumentException iae) {
                logger.debug("Invalid URI: {}", voTableURL, iae);
                // aladin bug: URI has an authority component
                try {
                    voTableURI = new URI(scheme, voTableURI.getPath(), null);
                } catch (URISyntaxException use) {
                    logger.error("invalid URI", use);
                    throw new SampException("Can not read the votable : " + voTableURL, use);
                }
            }
            try {
                voTableFile = new File(voTableURI);
            } catch (IllegalArgumentException iae) {
                logger.info("Invalid URI: {}", voTableURL, iae);
            }
        } else {
            final File file = FileUtils.getTempFile("votable-", ".vot");
            if (Http.download(voTableURI, file, false)) {
                voTableFile = file;
            }
        }
        if (voTableFile == null) {
            throw new SampException("Not supported URI scheme : " + voTableURL);
        }
        final String votable = FileUtils.readFile(voTableFile);
        logger.debug("votable :\n{}", votable);
        String searchCalVersion = null;
        // Note: Getting SearchCal version in sender meta data is not robust:
        // do not work with AppLauncher as senderMetadata corresponds to AppLauncher.metadata and not SearchCal !
        // TODO: use VOTABLE information instead !
        // Note: can be null if the client map is not up to date or the client disconnected:
        final Metadata senderMetadata = SampManager.getMetaData(senderId);
        if (senderMetadata != null) {
            logger.debug("senderMetadata: {}", senderMetadata);
            final String senderName = senderMetadata.getName();
            if ("searchcal".equalsIgnoreCase(senderName) || "getstar".equalsIgnoreCase(senderName)) {
                // SearchCal release > 4.4.1:
                searchCalVersion = senderMetadata.getString(SampMetaData.RELEASE_VERSION.id());
                if (searchCalVersion == null) {
                    // SearchCal release <= 4.4.1:
                    searchCalVersion = senderMetadata.getString("searchcal.version");
                }
            }
        }
        logger.debug("SearchCal version = {}", searchCalVersion);
        if (searchCalVersion == null) {
            // TODO: use 'undefined' as SearchCal version temporarly:
            if (!SearchCalVOTableHandler.processMessage(votable, "undefined")) {
                AnyVOTableHandler.processVOTable(votable, false);
            }
        } else {
            SearchCalVOTableHandler.processMessage(votable, searchCalVersion);
        }
    } catch (IOException ioe) {
        MessagePane.showErrorMessage("Can not read the votable :\n\n" + voTableURL);
        throw new SampException("Can not read the votable : " + voTableURL, ioe);
    }
}
Also used : SampException(org.astrogrid.samp.client.SampException) Metadata(org.astrogrid.samp.Metadata) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI) File(java.io.File)

Example 2 with SampException

use of org.astrogrid.samp.client.SampException in project aspro by JMMC-OpenDev.

the class ImageSampMessageHandler method processMessage.

/**
 * Implements message processing
 *
 * @param senderId public ID of sender client
 * @param message message with MType this handler is subscribed to
 * @throws SampException if any error occured while message processing
 */
@Override
protected void processMessage(final String senderId, final Message message) throws SampException {
    if (logger.isDebugEnabled()) {
        logger.debug("\tReceived '{}' message from '{}' : '{}'.", this.handledMType(), senderId, message);
    }
    // get url of file (locally stored):
    final String imgURL = (String) message.getRequiredParam("url");
    logger.info("processMessage: Image URL = {}", imgURL);
    if (imgURL == null) {
        throw new SampException("Can not get the url of the observation file");
    }
    URI imgURI;
    try {
        imgURI = new URI(imgURL);
    } catch (URISyntaxException use) {
        logger.error("invalid URI", use);
        throw new SampException("Can not read the image : " + imgURL, use);
    }
    // Guess file name:
    String fileName = FileUtils.getName(imgURL);
    if (fileName.isEmpty()) {
        fileName = StringUtils.replaceNonAlphaNumericCharsByUnderscore(imgURI.getPath());
    }
    // Download or get local file:
    File imgFile = null;
    try {
        final String scheme = imgURI.getScheme();
        if (scheme.equalsIgnoreCase("file")) {
            try {
                imgFile = new File(imgURI);
            } catch (IllegalArgumentException iae) {
                logger.debug("Invalid URI: {}", imgURI, iae);
                String path = imgURI.getPath();
                logger.debug("imgURI path: {}", path);
                final int nbSlash = countStartingSlash(path);
                logger.debug("nbSlash: {}", nbSlash);
                // always use '///path'
                // ds9 bug: URI file://localhost//tmp/...
                path = "///" + path.substring(nbSlash);
                logger.debug("fixed path: {}", path);
                // aladin bug: URI has an authority component
                try {
                    imgURI = new URI(scheme, path, null);
                } catch (URISyntaxException use) {
                    logger.error("invalid URI", use);
                    throw new SampException("Can not read the image : " + imgURL, use);
                }
            }
            try {
                imgFile = new File(imgURI);
            } catch (IllegalArgumentException iae) {
                logger.info("Invalid URI: {}", imgURI, iae);
            }
        } else {
            final File file = FileUtils.getTempFile("aspro-" + fileName, ".fits");
            if (Http.download(imgURI, file, false)) {
                imgFile = file;
            }
        }
        if (imgFile == null) {
            throw new SampException("Not supported URI scheme : " + imgURL);
        }
    } catch (IOException ioe) {
        MessagePane.showErrorMessage("Can not read the image :\n\n" + imgURL);
        throw new SampException("Can not read the image : " + imgURL, ioe);
    }
    final String urlFileName = fileName;
    final File localFile = imgFile;
    // Use invokeLater to avoid concurrency and ensure that
    // data model is modified and fire events using Swing EDT:
    SwingUtils.invokeEDT(new Runnable() {

        @Override
        public void run() {
            logger.info("localFile: {}", localFile);
            // Use main observation to get current target:
            final ObservationSetting observation = ObservationManager.getInstance().getMainObservation();
            // retrieve the selected target from its name:
            final Target target = observation.getTarget(observation.getSelectedTargetName());
            if (target != null) {
                logger.debug("target = {}", target);
                if (MessagePane.showConfirmMessage("Do you want to use the incoming image as an user model for the target '" + target.getName() + "' ?\n\n" + urlFileName)) {
                    // Ask the user to rename the file as the filename is generated (hash) in persistent folder:
                    final File imageFile = FileChooser.showSaveFileChooser("Choose (persistent) destination to write the Fits image file", null, MimeType.FITS_IMAGE, urlFileName);
                    // Cancel
                    if (imageFile == null) {
                        return;
                    }
                    // copy the file at the user location and name:
                    try {
                        FileUtils.copy(localFile, imageFile);
                    } catch (IOException ioe) {
                        MessagePane.showErrorMessage("Can not write the image to : " + imageFile.getAbsolutePath(), ioe);
                        return;
                    }
                    // Open target editor:
                    final TargetEditorDialog targetEditor = TargetEditorDialog.getTargetEditor();
                    if (targetEditor == null) {
                        // TODO: blocking so give the model file too as an optional argument
                        TargetEditorDialog.showEditor(target.getName(), TargetEditorDialog.TAB_MODELS, imageFile);
                    } else {
                        // refresh dialog to use the given model file for the current selected target
                        targetEditor.defineUserModel(imageFile);
                    }
                }
            }
        }
    });
}
Also used : SampException(org.astrogrid.samp.client.SampException) Target(fr.jmmc.aspro.model.oi.Target) ObservationSetting(fr.jmmc.aspro.model.oi.ObservationSetting) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI) File(java.io.File) TargetEditorDialog(fr.jmmc.aspro.gui.TargetEditorDialog)

Example 3 with SampException

use of org.astrogrid.samp.client.SampException in project aspro by JMMC-OpenDev.

the class ObservationSampMessageHandler method processMessage.

/**
 * Implements message processing
 *
 * @param senderId public ID of sender client
 * @param message message with MType this handler is subscribed to
 * @throws SampException if any error occured while message processing
 */
@Override
protected void processMessage(final String senderId, final Message message) throws SampException {
    if (logger.isDebugEnabled()) {
        logger.debug("\tReceived '{}' message from '{}' : '{}'.", this.handledMType(), senderId, message);
    }
    // get url of file (locally stored):
    final String obsURL = (String) message.getRequiredParam("url");
    logger.debug("processMessage: Observation URL = {}", obsURL);
    if (obsURL == null) {
        throw new SampException("Can not get the url of the observation file");
    }
    URI obsURI;
    try {
        obsURI = new URI(obsURL);
    } catch (URISyntaxException use) {
        logger.error("invalid URI", use);
        throw new SampException("Can not read the observation : " + obsURL, use);
    }
    File obsFile = null;
    try {
        final String scheme = obsURI.getScheme();
        if (scheme.equalsIgnoreCase("file")) {
            try {
                obsFile = new File(obsURI);
            } catch (IllegalArgumentException iae) {
                logger.debug("Invalid URI: {}", obsURL, iae);
                // aladin bug: URI has an authority component
                try {
                    obsURI = new URI(scheme, obsURI.getPath(), null);
                } catch (URISyntaxException use) {
                    logger.error("invalid URI", use);
                    throw new SampException("Can not read the votable : " + obsURL, use);
                }
            }
            try {
                obsFile = new File(obsURI);
            } catch (IllegalArgumentException iae) {
                logger.info("Invalid URI: {}", obsURL, iae);
            }
        } else {
            final File file = FileUtils.getTempFile("aspro-", ".asprox");
            if (Http.download(obsURI, file, false)) {
                obsFile = file;
            }
        }
        if (obsFile == null) {
            throw new SampException("Not supported URI scheme : " + obsURL);
        }
        // add targets only:
        TargetImporter.processObservation(obsFile, true);
    } catch (IOException ioe) {
        MessagePane.showErrorMessage("Can not read the votable :\n\n" + obsURL);
        throw new SampException("Can not read the votable : " + obsURL, ioe);
    }
}
Also used : SampException(org.astrogrid.samp.client.SampException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI) File(java.io.File)

Aggregations

File (java.io.File)3 IOException (java.io.IOException)3 URI (java.net.URI)3 URISyntaxException (java.net.URISyntaxException)3 SampException (org.astrogrid.samp.client.SampException)3 TargetEditorDialog (fr.jmmc.aspro.gui.TargetEditorDialog)1 ObservationSetting (fr.jmmc.aspro.model.oi.ObservationSetting)1 Target (fr.jmmc.aspro.model.oi.Target)1 Metadata (org.astrogrid.samp.Metadata)1