--- /dev/null
+/**
+ * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation; either version 2.1 of the License, or (at your option)
+ * any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+ * details.
+ */
+
+package com.pentila.entSavoie.internalMessaging.service.impl;
+
+import java.util.zip.Deflater;
+import java.util.zip.Inflater;
+
+import com.liferay.portal.kernel.log.Log;
+import com.liferay.portal.kernel.log.LogFactoryUtil;
+import com.pentila.entSavoie.internalMessaging.model.InternalContent;
+import com.pentila.entSavoie.internalMessaging.service.base.InternalContentLocalServiceBaseImpl;
+
+/**
+ * The implementation of the internal message content local service.
+ *
+ * @author Cédric Lecarpentier
+ */
+public class InternalContentLocalServiceImpl extends InternalContentLocalServiceBaseImpl {
+
+ private static Log logger = LogFactoryUtil.getLog(InternalContentLocalServiceImpl.class);
+
+ public InternalContent addContent(long messageId, String messageContent) {
+ try {
+ InternalContent internalContent = internalContentPersistence.create(messageId);
+ //String compressedStr = compressZlib(messageContent);
+ //logger.info("Message content compression from "+messageContent.length()+" to "+compressedStr.length());
+ internalContent.setMessageContent(messageContent);
+ internalContent = internalContentPersistence.updateImpl(internalContent, true);
+ return internalContent;
+ } catch (Exception e) {
+ logger.error("Error when creating an internal message content", e);
+ }
+ return null;
+ }
+
+ public String getContent(long messageId) {
+ try {
+ InternalContent internalContent = internalContentPersistence.fetchBymessageId(messageId);
+ //String uncompressedStr = decompressZlib(internalContent.getMessageContent());
+ //logger.info("Message content decompression from "+internalContent.getMessageContent().length()+" to "+uncompressedStr.length());
+ return internalContent.getMessageContent();
+ } catch (Exception e) {
+ logger.error("Error when fetching an internal message content with messageId "+messageId, e);
+ }
+ return "";
+ }
+
+ private static int MAX_CONTENT_LENGTH = 10000000; // 10Mo should be enough
+
+ private static String compressZlib (String input) {
+
+ logger.info("Compressing using ZLib ...");
+ try {
+ Deflater compresser = new Deflater();
+ compresser.setInput(input.getBytes());
+ byte[] result = new byte[MAX_CONTENT_LENGTH];
+ int length = compresser.deflate(result, 0, input.getBytes().length);
+ compresser.end();
+ return new String(result, 0, length);
+ } catch (Exception e) {
+ logger.error("Error when compressing ZLib ", e);
+ }
+ return "";
+ }
+
+ private static String decompressZlib (String input) {
+
+ logger.info("Decompressing using ZLib ...");
+ try {
+ Inflater decompresser = new Inflater();
+ decompresser.setInput(input.getBytes());
+ byte[] result = new byte[MAX_CONTENT_LENGTH];
+ decompresser.inflate(result);
+ decompresser.end();
+ return new String(result);
+ } catch (java.util.zip.DataFormatException e) {
+ logger.error("Error when decompressing ZLib ", e);
+ }
+ return "";
+ }
+
+
+}
\ No newline at end of file