public static Class<IsRODAObject> getSelectedClassFromString(String selectedClass) throws GenericException { try { Class<?> clazz = Class.forName(selectedClass); if (IsRODAObject.class.isAssignableFrom(clazz)) { return (Class<IsRODAObject>) clazz; } else { throw new GenericException("Error while getting class from string"); } } catch (ClassNotFoundException e) { throw new GenericException("Error while getting class from string"); } }
private static String decodeId(String id) throws GenericException { if (id != null) { return id.replaceAll(SEPARATOR_URL_ENCODED, SEPARATOR); } else { throw new GenericException("Was trying to decode an 'id' but it is NULL"); } }
public static <T> T getObjectFromXML(String xml, Class<T> objectClass) throws GenericException { JAXBContext jaxbContext; try { jaxbContext = JAXBContext.newInstance(objectClass); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); StringReader reader = new StringReader(xml); return (T) unmarshaller.unmarshal(reader); } catch (JAXBException e) { throw new GenericException(e); } } }
public static JsonNode parseJson(InputStream json) throws GenericException { try { ObjectMapper mapper = new ObjectMapper(new JsonFactory()); return mapper.readTree(json); } catch (IOException e) { throw new GenericException(JSON_ERROR_MESSAGE, e); } finally { IOUtils.closeQuietly(json); } }
public static void writeObjectToFile(Object object, Path file) throws GenericException { try { String json = getJsonFromObject(object); if (json != null) { Files.write(file, json.getBytes(), StandardOpenOption.CREATE); } } catch (IOException e) { throw new GenericException("Error writing object, as json, to file", e); } }
public static <T> List<T> getListFromJson(String json, Class<T> objectClass) throws GenericException { try { ObjectMapper mapper = new ObjectMapper(new JsonFactory()); TypeFactory t = TypeFactory.defaultInstance(); return mapper.readValue(json, t.constructCollectionType(ArrayList.class, objectClass)); } catch (IOException e) { throw new GenericException(JSON_ERROR_MESSAGE, e); } }
public static <T> T getObjectFromXML(InputStream xml, Class<T> objectClass) throws GenericException { T ret; try { String xmlString = IOUtils.toString(xml, RodaConstants.DEFAULT_ENCODING); ret = getObjectFromXML(xmlString, objectClass); } catch (IOException e) { throw new GenericException(e); } finally { IOUtils.closeQuietly(xml); } return ret; }
private static Map<String, Field> getFields(SolrClient client, String collectionName) throws GenericException { SchemaRequest.Fields fields = new SchemaRequest.Fields(); FieldsResponse response; try { response = fields.process(client, collectionName); return response.getFields().stream().map(f -> new Field(f)) .collect(Collectors.toMap(Field::getName, Function.identity())); } catch (SolrServerException | IOException e) { throw new GenericException("Could not get schema fields", e); } }
public void failOnError() throws GenericException { if (!hasNoExceptions()) { if (list.size() == 1 && list.get(0).getExceptions().size() == 1) { // if there is one and only one exception throw it throw new GenericException(list.get(0).getExceptions().get(0)); } else { LOGGER.error(printStackTraces()); throw MultipleGenericException.build(list); } } } }
private static Set<CopyField> getCopyFields(SolrClient client, String collectionName) throws GenericException { SchemaRequest.CopyFields fields = new SchemaRequest.CopyFields(); CopyFieldsResponse response; try { response = fields.process(client, collectionName); return response.getCopyFields().stream().map(f -> new CopyField(f)).collect(Collectors.toSet()); } catch (SolrServerException | IOException e) { throw new GenericException("Could not get schema dynamic fields", e); } }
protected static XsltExecutable createEventTransformer(String path) throws SaxonApiException, GenericException { try (InputStream transformerStream = RodaCoreFactory.getConfigurationFileAsStream(path)) { if (transformerStream == null) { throw new GenericException("Could not find stylesheet nor fallback at path=" + path); } XsltCompiler compiler = PROCESSOR.newXsltCompiler(); compiler.setURIResolver(new RodaURIFileResolver()); return compiler.compile(new StreamSource(transformerStream)); } catch (IOException e) { throw new GenericException(e); } }
public static String executeHandlebars(String template, Map<String, ?> scopes) throws GenericException { try { Template templ = getHandlebars().compileInline(template); return templ.apply(scopes); } catch (IOException e) { throw new GenericException(e); } }
public void deleteNotification(String notificationId) throws GenericException, NotFoundException, AuthorizationDeniedException { RodaCoreFactory.checkIfWriteIsAllowedAndIfFalseThrowException(nodeType); try { StoragePath notificationPath = ModelUtils.getNotificationStoragePath(notificationId); storage.deleteResource(notificationPath); notifyNotificationDeleted(notificationId).failOnError(); } catch (RequestNotValidException e) { LOGGER.error("Error deleting notification", e); throw new GenericException(e); } }
public void clearRepositoryEventIndex() throws GenericException, AuthorizationDeniedException { RodaCoreFactory.checkIfWriteIsAllowedAndIfFalseThrowException(nodeType); String indexName = RodaConstants.INDEX_PRESERVATION_EVENTS; try { getSolrClient().deleteByQuery(indexName, RodaConstants.PRESERVATION_EVENT_OBJECT_CLASS + ":" + PreservationMetadataEventClass.REPOSITORY.toString()); getSolrClient().commit(indexName); } catch (SolrServerException | IOException e) { LOGGER.error("Error cleaning up index {}", indexName, e); throw new GenericException("Error cleaning up index " + indexName, e); } }
public void clearAIPEventIndex() throws GenericException, AuthorizationDeniedException { RodaCoreFactory.checkIfWriteIsAllowedAndIfFalseThrowException(nodeType); String indexName = RodaConstants.INDEX_PRESERVATION_EVENTS; try { getSolrClient().deleteByQuery(indexName, "*:* -" + RodaConstants.PRESERVATION_EVENT_OBJECT_CLASS + ":" + PreservationMetadataEventClass.REPOSITORY.toString()); getSolrClient().commit(indexName); } catch (SolrServerException | IOException e) { LOGGER.error("Error cleaning up index {}", indexName, e); throw new GenericException("Error cleaning up index " + indexName, e); } }
public Job retrieveJob(String jobId) throws RequestNotValidException, GenericException, NotFoundException, AuthorizationDeniedException { StoragePath jobPath = ModelUtils.getJobStoragePath(jobId); Binary binary = storage.getBinary(jobPath); Job ret; try (InputStream inputStream = binary.getContent().createInputStream()) { ret = JsonUtils.getObjectFromJson(inputStream, Job.class); } catch (IOException | GenericException e) { throw new GenericException("Error reading job: " + jobId, e); } return ret; }
public RiskIncidence retrieveRiskIncidence(String incidenceId) throws RequestNotValidException, GenericException, NotFoundException, AuthorizationDeniedException { StoragePath riskIncidencePath = ModelUtils.getRiskIncidenceStoragePath(incidenceId); Binary binary = storage.getBinary(riskIncidencePath); RiskIncidence ret; try (InputStream inputStream = binary.getContent().createInputStream()) { ret = JsonUtils.getObjectFromJson(inputStream, RiskIncidence.class); } catch (IOException e) { throw new GenericException("Error reading risk incidence", e); } return ret; }
public Report retrieveJobReport(String jobId, String jobReportId) throws RequestNotValidException, GenericException, NotFoundException, AuthorizationDeniedException { StoragePath jobReportPath = ModelUtils.getJobReportStoragePath(jobId, jobReportId); Binary binary = storage.getBinary(jobReportPath); Report ret; try (InputStream inputStream = binary.getContent().createInputStream()) { ret = JsonUtils.getObjectFromJson(inputStream, Report.class); } catch (IOException e) { throw new GenericException("Error reading job report", e); } return ret; }
public boolean isInternal(String username) throws GenericException, NotFoundException { final CoreSession session = this.service.getAdminSession(); Entry entry; try { entry = session.lookup(new Dn(getUserDN(username))); return entry.containsAttribute(USER_PASSWORD); } catch (LdapNoSuchObjectException e) { throw new NotFoundException(username); }catch (LdapException e) { throw new GenericException(e); } }
public void optimizeAIPs() throws GenericException, AuthorizationDeniedException { RodaCoreFactory.checkIfWriteIsAllowedAndIfFalseThrowException(nodeType); try { getSolrClient().optimize(RodaConstants.INDEX_AIP); getSolrClient().optimize(RodaConstants.INDEX_FILE); getSolrClient().optimize(RodaConstants.INDEX_REPRESENTATION); getSolrClient().optimize(RodaConstants.INDEX_PRESERVATION_EVENTS); getSolrClient().optimize(RodaConstants.INDEX_PRESERVATION_AGENTS); } catch (SolrServerException | IOException e) { throw new GenericException("Error while optimizing indexes", e); } }