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;
}
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);
}
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;
}
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);
}
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);
}
Aggregations