Search in sources :

Example 1 with ResourceNotFoundException

use of org.fao.geonet.api.exception.ResourceNotFoundException in project core-geonetwork by geonetwork.

the class LocaleRedirects method checkPortalExist.

/**
 * Check that the requested portal exist.
 *If not return the list of existing ones if requested one is not found.
 *
 * @param portal
 * @throws ResourceNotFoundException
 */
private boolean checkPortalExist(String portal, boolean throwException) throws ResourceNotFoundException {
    if (portal == null || NodeInfo.DEFAULT_NODE.equals(portal)) {
        // This is the default node
        return true;
    }
    final Source one = sourceRepository.findOne(portal);
    if (one == null) {
        List<String> portalList = new ArrayList<>();
        portalList.add(NodeInfo.DEFAULT_NODE);
        sourceRepository.findByType(SourceType.subportal).forEach(e -> {
            portalList.add(e.getUuid());
        });
        if (throwException) {
            throw new ResourceNotFoundException(String.format("No portal found with id '%s'. The list of available portals are: %s", portal, portalList.toString()));
        }
        return false;
    }
    return true;
}
Also used : ArrayList(java.util.ArrayList) ResourceNotFoundException(org.fao.geonet.api.exception.ResourceNotFoundException) Source(org.fao.geonet.domain.Source)

Example 2 with ResourceNotFoundException

use of org.fao.geonet.api.exception.ResourceNotFoundException in project core-geonetwork by geonetwork.

the class InspireAtomUtil method getDatasetFeed.

public static Element getDatasetFeed(final ServiceContext context, final String spIdentifier, final String spNamespace, final Map<String, Object> params, String requestedLanguage) throws Exception {
    ServiceConfig config = new ServiceConfig();
    config.setValue(Geonet.SearchConfig.SEARCH_IGNORE_PORTAL_FILTER_OPTION, "true");
    SearchManager searchMan = context.getBean(SearchManager.class);
    // Search for the dataset identified by spIdentifier
    AbstractMetadata datasetMd = null;
    Document dsLuceneSearchParams = createDefaultLuceneSearcherParams();
    dsLuceneSearchParams.getRootElement().addContent(new Element("identifier").setText(spIdentifier));
    if (StringUtils.isNotBlank(spNamespace)) {
        dsLuceneSearchParams.getRootElement().addContent(new Element("identifierNamespace").setText(spNamespace));
    }
    dsLuceneSearchParams.getRootElement().addContent(new Element("type").setText("dataset"));
    try (MetaSearcher searcher = searchMan.newSearcher(SearcherType.LUCENE, Geonet.File.SEARCH_LUCENE)) {
        searcher.search(context, dsLuceneSearchParams.getRootElement(), config);
        Element searchResult = searcher.present(context, dsLuceneSearchParams.getRootElement(), config);
        XPath xp = XPath.newInstance("//response/metadata/geonet:info/uuid/text()");
        xp.addNamespace("geonet", "http://www.fao.org/geonetwork");
        if (searchResult.getContentSize() == 0) {
            if (StringUtils.isNotBlank(spNamespace)) {
                dsLuceneSearchParams.getRootElement().removeChild("identifierNamespace");
                searcher.search(context, dsLuceneSearchParams.getRootElement(), config);
                searchResult = searcher.present(context, dsLuceneSearchParams.getRootElement(), config);
            }
        }
        if (searchResult.getContentSize() > 0) {
            Text uuidTxt = (Text) xp.selectSingleNode(new Document(searchResult));
            String datasetMdUuid = uuidTxt.getText();
            IMetadataUtils repo = context.getBean(IMetadataUtils.class);
            datasetMd = repo.findOneByUuid(datasetMdUuid);
        }
    } finally {
        if (datasetMd == null) {
            throw new ResourceNotFoundException(String.format("No dataset found with resource identifier '%s'. Check that a record exist with hierarchy level is set to 'dataset' with a resource identifier set to '%s'.", spIdentifier, spIdentifier));
        }
    }
    // check user's rights
    try {
        Lib.resource.checkPrivilege(context, String.valueOf(datasetMd.getId()), ReservedOperation.view);
    } catch (Exception e) {
        // This does not return a 403 as expected Oo
        throw new UnAuthorizedException("Access denied to metadata " + datasetMd.getUuid(), e);
    }
    String serviceMdUuid = null;
    Document luceneParamSearch = createDefaultLuceneSearcherParams();
    luceneParamSearch.getRootElement().addContent(new Element("operatesOn").setText(datasetMd.getUuid() + "* or " + spIdentifier + "*"));
    luceneParamSearch.getRootElement().addContent(new Element("serviceType").setText("download"));
    try (MetaSearcher searcher = searchMan.newSearcher(SearcherType.LUCENE, Geonet.File.SEARCH_LUCENE)) {
        searcher.search(context, luceneParamSearch.getRootElement(), config);
        Element searchResult = searcher.present(context, luceneParamSearch.getRootElement(), config);
        XPath xp = XPath.newInstance("//response/metadata/geonet:info/uuid/text()");
        xp.addNamespace("geonet", "http://www.fao.org/geonetwork");
        Text uuidTxt = (Text) xp.selectSingleNode(new Document(searchResult));
        serviceMdUuid = uuidTxt.getText();
    } finally {
        if (serviceMdUuid == null) {
            throw new ResourceNotFoundException(String.format("No service operating the dataset '%s'. Check that a service is attached to that dataset and that its service type is set to 'download'.", datasetMd.getUuid()));
        }
    }
    DataManager dm = context.getBean(DataManager.class);
    Element md = datasetMd.getXmlData(false);
    if (StringUtils.isBlank(requestedLanguage)) {
        String schema = dm.getMetadataSchema(String.valueOf(datasetMd.getId()));
        String defaultLanguage = dm.extractDefaultLanguage(schema, md);
        requestedLanguage = XslUtil.twoCharLangCode(defaultLanguage);
    }
    Element inputDoc = InspireAtomUtil.prepareDatasetFeedEltBeforeTransform(md, serviceMdUuid);
    params.put("requestedLanguage", requestedLanguage);
    return InspireAtomUtil.convertDatasetMdToAtom("iso19139", inputDoc, dm, params);
}
Also used : XPath(org.jdom.xpath.XPath) SearchManager(org.fao.geonet.kernel.search.SearchManager) IMetadataUtils(org.fao.geonet.kernel.datamanager.IMetadataUtils) Element(org.jdom.Element) MetaSearcher(org.fao.geonet.kernel.search.MetaSearcher) Text(org.jdom.Text) DataManager(org.fao.geonet.kernel.DataManager) Document(org.jdom.Document) UnAuthorizedException(org.fao.geonet.exceptions.UnAuthorizedException) AbstractMetadata(org.fao.geonet.domain.AbstractMetadata) ResourceNotFoundException(org.fao.geonet.api.exception.ResourceNotFoundException) UnAuthorizedException(org.fao.geonet.exceptions.UnAuthorizedException) ServiceConfig(jeeves.server.ServiceConfig) ResourceNotFoundException(org.fao.geonet.api.exception.ResourceNotFoundException)

Example 3 with ResourceNotFoundException

use of org.fao.geonet.api.exception.ResourceNotFoundException in project core-geonetwork by geonetwork.

the class InspireAtomUtil method prepareServiceFeedEltBeforeTransform.

public static Element prepareServiceFeedEltBeforeTransform(final String schema, final Element md, final DataManager dataManager) throws Exception {
    List<String> datasetsUuids = extractRelatedDatasetsIdentifiers(schema, md, dataManager);
    Element root = new Element("root");
    Element serviceElt = new Element("service");
    Element datasetElt = new Element("datasets");
    root.addContent(serviceElt);
    md.addContent(datasetElt);
    for (String uuid : datasetsUuids) {
        String id = dataManager.getMetadataId(uuid);
        if (StringUtils.isEmpty(id))
            throw new ResourceNotFoundException(String.format("Dataset '%s' attached to the requested service was not found. Check the link between the 2 records (see operatesOn element).", uuid));
        Element ds = dataManager.getMetadata(id);
        datasetElt.addContent(ds);
    }
    serviceElt.addContent(md);
    return root;
}
Also used : Element(org.jdom.Element) ResourceNotFoundException(org.fao.geonet.api.exception.ResourceNotFoundException)

Example 4 with ResourceNotFoundException

use of org.fao.geonet.api.exception.ResourceNotFoundException in project core-geonetwork by geonetwork.

the class AtomPredefinedFeed method getServiceFeed.

private Element getServiceFeed(ServiceContext context, final String uuid, final String language) throws Exception {
    Log.debug(Geonet.ATOM, "Processing service feed  ( uuid : " + uuid + " )");
    SettingManager sm = context.getBean(SettingManager.class);
    DataManager dm = context.getBean(DataManager.class);
    // Check if metadata exists
    String id = dm.getMetadataId(uuid);
    if (StringUtils.isEmpty(id))
        throw new MetadataNotFoundEx(uuid);
    // check user's rights
    try {
        Lib.resource.checkPrivilege(context, id, ReservedOperation.view);
    } catch (Exception e) {
        throw new UnAuthorizedException("Access denied to metadata " + id, e);
    }
    // Check if it is a service metadata
    Element md = dm.getMetadata(id);
    String schema = dm.getMetadataSchema(id);
    if (!InspireAtomUtil.isServiceMetadata(dm, schema, md)) {
        throw new ResourceNotFoundException("No service metadata found with uuid:" + uuid);
    }
    String defaultLanguage = dm.extractDefaultLanguage(schema, md);
    String requestedLanguage = StringUtils.isNotBlank(language) ? language : XslUtil.twoCharLangCode(defaultLanguage);
    Element inputDoc = InspireAtomUtil.prepareServiceFeedEltBeforeTransform(schema, md, dm);
    Map<String, Object> params = getDefaultXSLParams(sm, context, requestedLanguage);
    return InspireAtomUtil.convertDatasetMdToAtom("iso19139", inputDoc, dm, params);
}
Also used : SettingManager(org.fao.geonet.kernel.setting.SettingManager) MetadataNotFoundEx(org.fao.geonet.exceptions.MetadataNotFoundEx) Element(org.jdom.Element) DataManager(org.fao.geonet.kernel.DataManager) UnAuthorizedException(org.fao.geonet.exceptions.UnAuthorizedException) ResourceNotFoundException(org.fao.geonet.api.exception.ResourceNotFoundException) ResourceNotFoundException(org.fao.geonet.api.exception.ResourceNotFoundException) UnAuthorizedException(org.fao.geonet.exceptions.UnAuthorizedException)

Example 5 with ResourceNotFoundException

use of org.fao.geonet.api.exception.ResourceNotFoundException in project core-geonetwork by geonetwork.

the class AtomPredefinedFeed method getOpenSearchDescription.

private Element getOpenSearchDescription(ServiceContext context, final String uuid) throws Exception {
    Log.debug(Geonet.ATOM, "Processing openseachdescription  ( uuid : " + uuid + " )");
    SettingManager sm = context.getBean(SettingManager.class);
    DataManager dm = context.getBean(DataManager.class);
    // Check if metadata exists
    String id = dm.getMetadataId(uuid);
    if (StringUtils.isEmpty(id))
        throw new MetadataNotFoundEx(uuid);
    // check user's rights
    try {
        Lib.resource.checkPrivilege(context, id, ReservedOperation.view);
    } catch (Exception e) {
        throw new UnAuthorizedException("Access denied to metadata " + id, e);
    }
    // Check if it is a service metadata
    Element md = dm.getMetadata(id);
    String schema = dm.getMetadataSchema(id);
    if (!InspireAtomUtil.isServiceMetadata(dm, schema, md)) {
        throw new ResourceNotFoundException("No service metadata found with uuid:" + uuid);
    }
    String defaultLanguage = dm.extractDefaultLanguage(schema, md);
    Map<String, Object> params = getDefaultXSLParams(sm, context, XslUtil.twoCharLangCode(defaultLanguage));
    Element inputDoc = InspireAtomUtil.prepareOpenSearchDescriptionEltBeforeTransform(context, params, uuid, schema, InspireAtomUtil.convertDatasetMdToAtom("iso19139", InspireAtomUtil.prepareServiceFeedEltBeforeTransform(schema, md, dm), dm, params), defaultLanguage, dm);
    return InspireAtomUtil.convertServiceMdToOpenSearchDescription(context, inputDoc, params);
}
Also used : SettingManager(org.fao.geonet.kernel.setting.SettingManager) MetadataNotFoundEx(org.fao.geonet.exceptions.MetadataNotFoundEx) Element(org.jdom.Element) DataManager(org.fao.geonet.kernel.DataManager) UnAuthorizedException(org.fao.geonet.exceptions.UnAuthorizedException) ResourceNotFoundException(org.fao.geonet.api.exception.ResourceNotFoundException) ResourceNotFoundException(org.fao.geonet.api.exception.ResourceNotFoundException) UnAuthorizedException(org.fao.geonet.exceptions.UnAuthorizedException)

Aggregations

ResourceNotFoundException (org.fao.geonet.api.exception.ResourceNotFoundException)63 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)32 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)31 ApiOperation (io.swagger.annotations.ApiOperation)28 ApiResponses (io.swagger.annotations.ApiResponses)26 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)22 ServiceContext (jeeves.server.context.ServiceContext)18 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)18 Element (org.jdom.Element)15 Path (java.nio.file.Path)13 AbstractMetadata (org.fao.geonet.domain.AbstractMetadata)13 IOException (java.io.IOException)12 ApplicationContext (org.springframework.context.ApplicationContext)10 NotAllowedException (org.fao.geonet.api.exception.NotAllowedException)8 ResponseEntity (org.springframework.http.ResponseEntity)8 UserSession (jeeves.server.UserSession)6 ArrayList (java.util.ArrayList)5 Selection (org.fao.geonet.domain.Selection)5 Source (org.fao.geonet.domain.Source)5 UserSavedSelection (org.fao.geonet.domain.UserSavedSelection)5