@Override public <T> T deserialize(final String content, final Class<T> aClass) throws SerializationException { try { return MAPPER.readValue(content, aClass); } catch (IOException e) { LOG.error("Error deserializing.", e); throw new SerializationException(e); } }
@Override public String serialize(final Object item) throws SerializationException { try { LOG.debug("Serializing a {}", item.getClass()); return MAPPER.writeValueAsString(item); } catch (JsonProcessingException e) { LOG.error("Error serializing.", e); throw new SerializationException(e); } } }
@Override public <T extends Page> T getPage(final String uri, final Class<T> pageModel) throws FactoryException { if (!pageModel.getClass().isInstance(Page.class)) { throw new SerializationException("Given model class does not implement the Page interface"); } return getPage(uri, null, pageModel); }
/** * Dcompresses a byte array representing a GZip-compressed string back into a String. * * @param bytes the byte array to decompress * @return the deserialized object of the given class type * @throws SerializationException if something goes wrong with the streams */ public static String decompressGZip(byte[] bytes) throws SerializationException { String result = null; ByteArrayInputStream bais = null; GZIPInputStream gis = null; try { bais = new ByteArrayInputStream(bytes); gis = new GZIPInputStream(bais); result = IOUtils.toString(gis); } catch (IOException ioe) { LOG.error("Decompression failed.", ioe); throw new SerializationException("Failed to decompress byte array", ioe); } finally { IOUtils.closeQuietly(gis); IOUtils.closeQuietly(bais); } return result; }
/** * Compresses a given object to a GZipped byte array. * * @param object the object to encode * @return byte[] representing the compressed object bytes * @throws SerializationException if something goes wrong with the streams */ public static <T> byte[] compressGZipGeneric(T object) throws SerializationException { ByteArrayOutputStream baos = null; GZIPOutputStream gos = null; ObjectOutputStream oos = null; try { baos = new ByteArrayOutputStream(); gos = new GZIPOutputStream(baos); oos = new ObjectOutputStream(gos); oos.writeObject(object); gos.close(); return baos.toByteArray(); } catch (IOException ioe) { LOG.error("Compression failed.", ioe); throw new SerializationException("Failed to compres object", ioe); } finally { IOUtils.closeQuietly(oos); IOUtils.closeQuietly(gos); IOUtils.closeQuietly(baos); } }
/** * Compresses a given content to a GZipped byte array. * * @param content the content to encode * @return byte[] representing the compressed content bytes * @throws SerializationException if something goes wrong with the streams */ public static byte[] compressGZip(String content) throws SerializationException { ByteArrayOutputStream baos = null; GZIPOutputStream gos = null; try { baos = new ByteArrayOutputStream(); gos = new GZIPOutputStream(baos); gos.write(content.getBytes(CharEncoding.UTF_8)); gos.close(); return baos.toByteArray(); } catch (IOException ioe) { LOG.error("String compression failed.", ioe); throw new SerializationException("Failed to compress String", ioe); } finally { IOUtils.closeQuietly(gos); IOUtils.closeQuietly(baos); } }
/** * Dcompresses a byte array representing a GZip-compressed object into an object of the given class type. * * @param bytes the byte array to decompress * @param <T> the class type to deserialize the byte array into * @return the deserialized object of the given class type * @throws SerializationException if something goes wrong with the streams */ public static <T> T decompressGZipGeneric(byte[] bytes) throws SerializationException { T result = null; ByteArrayInputStream bais = null; GZIPInputStream gis = null; ObjectInputStream ois = null; try { bais = new ByteArrayInputStream(bytes); gis = new GZIPInputStream(bais); ois = new ObjectInputStream(gis); result = (T) ois.readObject(); } catch (ClassNotFoundException | IOException e) { LOG.error("Decompression failed.", e); throw new SerializationException("Object failed decompression", e); } finally { IOUtils.closeQuietly(ois); IOUtils.closeQuietly(gis); IOUtils.closeQuietly(bais); } return result; }
/** * Deserializes a JSON encoded String into an object of the given type, which must * derive from the Page interface * * @param source String representing the JSON encoded object * @param clazz Class representing the implementation type to deserialize into * @return the deserialized object */ @Override public <T extends Page> T deserialize(final String source, final Class<? extends T> clazz) throws FactoryException { DataBinder binder = selectDataBinder(source); if (binder == null) { throw new SerializationException("Unable to select databinder."); } return binder.buildPage(source, clazz); }
@Override public String resolve(String componentURI, String pageURI) throws ItemNotFoundException, SerializationException { String key; if (!StringUtils.isEmpty(pageURI)) { key = getCacheKey(componentURI, pageURI); } else { key = getCacheKey(componentURI); } final CacheElement<String> cacheElement = cacheProvider.loadPayloadFromLocalCache(key); String result; if (!validInCache(cacheElement)) { if (!StringUtils.isEmpty(pageURI)) { result = linkProvider.resolveComponentFromPage(componentURI, pageURI); } else { result = linkProvider.resolveComponent(componentURI); } try { result = addToCache(componentURI, key, cacheElement, result); } catch (ParseException e) { String message = String.format("Invalid ComponentURI %s", componentURI); LOG.error(message); throw new SerializationException(message, e); } } else { result = cacheElement.getPayload(); LOG.debug("Return link url: {} for uri: {} from cache", result, componentURI); } return result; }
/** * Retrieves content of a Page by looking the page up by its item id and Publication id. * * @param id int representing the page item id * @param publication int representing the Publication id of the page * @return String representing the content of the Page * @throws ItemNotFoundException if the requested page does not exist */ @Override public String getPageContentById(int id, int publication) throws ItemNotFoundException, SerializationException { CharacterData data = null; try { PageDAO pageDAO = (PageDAO) StorageManagerFactory.getDAO(publication, StorageTypeMapping.PAGE); data = pageDAO.findByPrimaryKey(publication, id); } catch (StorageException e) { LOG.error(e.getMessage(), e); } if (data == null) { throw new ItemNotFoundException("Unable to find page by id '" + id + "' and publication '" + publication + "'."); } try { return decodeAndDecompressContent(convertStreamToString(data.getInputStream())); } catch (IOException e) { throw new SerializationException(e); } }
/** * Retrieves content of a Page by looking the page up by its item id and Publication id. * * @param id int representing the page item id * @param publication int representing the Publication id of the page * @return String representing the content of the Page * @throws ItemNotFoundException if the requested page does not exist */ @Override public String getPageContentById(int id, int publication) throws ItemNotFoundException, SerializationException { final CharacterData data = PAGE_CONTENT_RETRIEVER.getPageContent(publication, id); if (data == null) { throw new ItemNotFoundException("Unable to find page by id '" + id + "' and publication '" + publication + "'."); } try { return decodeAndDecompressContent(convertStreamToString(data.getInputStream())); } catch (IOException e) { throw new SerializationException(e); } }
throw new SerializationException("Given model class does not implement the Page interface"); throw new SerializationException(e);
} catch (ParseException e) { LOG.error(e.getLocalizedMessage(), e); throw new SerializationException(e);