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"); } }
@SuppressWarnings("unchecked") public static <T extends IsRODAObject> Class<T> parseClass(String classNameToReturn) throws GenericException { Class<T> classToReturn; try { classToReturn = (Class<T>) Class.forName(classNameToReturn); } catch (ClassNotFoundException e) { throw new GenericException("Could not find class " + classNameToReturn); } return classToReturn; } }
private static String encodeId(String id) throws GenericException { if (id != null) { return id.replaceAll(SEPARATOR_REGEX, SEPARATOR_URL_ENCODED); } else { throw new GenericException("Was trying to encode 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 <T> T getObjectFromJson(String json, Class<T> objectClass) throws GenericException { try { ObjectMapper mapper = new ObjectMapper(new JsonFactory()); return mapper.readValue(json, objectClass); } catch (IOException e) { throw new GenericException(JSON_ERROR_MESSAGE, e); } }
public static JsonNode parseJson(String json) throws GenericException { try { ObjectMapper mapper = new ObjectMapper(new JsonFactory()); return mapper.readTree(json); } catch (IOException e) { throw new GenericException(JSON_ERROR_MESSAGE, 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 String getXMLFromObject(Object object) throws GenericException { String ret = null; JAXBContext jaxbContext; try { jaxbContext = JAXBContext.newInstance(object.getClass()); Marshaller marshaller = jaxbContext.createMarshaller(); StringWriter writer = new StringWriter(); marshaller.marshal(object, writer); ret = writer.toString(); } catch (JAXBException e) { throw new GenericException(e); } return ret; }
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 Class<IsIndexed> getIsIndexedSelectedClassFromString(String selectedClass) throws GenericException { try { Class<?> clazz = Class.forName(selectedClass); if (IsIndexed.class.isAssignableFrom(clazz)) { try { return (Class<IsIndexed>) clazz; } catch (ClassCastException e) { LOGGER.error("Error while casting class to IsIndexed", e); // do nothing and let exception in the end of the method be thrown } } } catch (ClassNotFoundException e) { // do nothing and let exception in the end of the method be thrown LOGGER.error("Class not found", e); } throw new GenericException("Error while getting class from string"); }
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; }
public static <T> T readObjectFromFile(Path jsonFile, Class<T> objectClass) throws GenericException { try (InputStream stream = Files.newInputStream(jsonFile)) { return getObjectFromJson(stream, objectClass); } catch (IOException e) { throw new GenericException(e); } }
public static void appendObjectToFile(Object object, Path file) throws GenericException { try { String json = getJsonFromObject(object) + "\n"; Files.write(file, json.getBytes(), StandardOpenOption.APPEND); } catch (IOException e) { throw new GenericException("Error writing object, as json, to file", e); } }
public static <T> T getObjectFromJson(InputStream json, Class<T> objectClass) throws GenericException { T ret; try { String jsonString = IOUtils.toString(json, RodaConstants.DEFAULT_ENCODING); ret = getObjectFromJson(jsonString, objectClass); } catch (IOException e) { throw new GenericException(e); } finally { IOUtils.closeQuietly(json); } return ret; }
public static <T> T getObjectFromJson(Path json, Class<T> objectClass) throws GenericException { T ret; InputStream stream = null; try { stream = Files.newInputStream(json); String jsonString = IOUtils.toString(stream, RodaConstants.DEFAULT_ENCODING); ret = getObjectFromJson(jsonString, objectClass); } catch (IOException e) { throw new GenericException(JSON_ERROR_MESSAGE, e); } finally { IOUtils.closeQuietly(stream); } return ret; }
public static Long recursivelyCountPath(Path directoryPath) throws NotFoundException, GenericException { // starting at -1 because the walk counts the directory itself Long count = -1L; try (Stream<Path> walk = Files.walk(directoryPath)) { final Iterator<Path> pathIterator = walk.iterator(); while (pathIterator.hasNext()) { count++; pathIterator.next(); } } catch (NoSuchFileException e) { throw new NotFoundException("Could not list contents of entity because it doesn't exist: " + directoryPath); } catch (IOException e) { throw new GenericException("Could not list contents of entity at: " + directoryPath, e); } return count; }
private void initialize(Path path) throws GenericException { if (!FSUtils.exists(path)) { if (Files.isWritable(path.getParent())) { try { Files.createDirectories(path); } catch (IOException e) { throw new GenericException("Could not create path " + path, e); } } else { LOGGER.warn("Trying to initialize {} but parent is not writable", path); } } else if (!FSUtils.isDirectory(path)) { throw new GenericException("Path is not a directory " + path); } else if (!Files.isReadable(path)) { throw new GenericException("Cannot read from path " + path); } else { // do nothing } }
public static void commit(SolrClient index, List<Class<? extends IsIndexed>> resultClasses) throws GenericException { List<String> collections = new ArrayList<>(); for (Class<? extends IsIndexed> resultClass : resultClasses) { try { collections.add(SolrCollectionRegistry.getIndexName(resultClass)); } catch (NotSupportedException e) { throw new GenericException(e); } } commit(index, collections.toArray(new String[] {})); }