Using JSON serilization (via jackson). refs #9036 #9034

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-publishing/document-store-lib-accounting-service@152699 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2017-09-06 08:53:40 +00:00
parent 9e079107f5
commit 0c119f0bfb
5 changed files with 38 additions and 28 deletions

View File

@ -3,8 +3,10 @@
<ReleaseNotes> <ReleaseNotes>
<Changeset component="org.gcube.data-publishing.document-store-lib-accounting-service.1-1-0" date="${buildDate}"> <Changeset component="org.gcube.data-publishing.document-store-lib-accounting-service.1-1-0" date="${buildDate}">
<Change>Added HTTPS support #8758</Change> <Change>Added HTTPS support #8758</Change>
<Change>Added HTTP Redirection support #8758</Change>
<Change>Added HAProxy discovery support #8758</Change> <Change>Added HAProxy discovery support #8758</Change>
<Change>Restored Fallback capabilities #9016</Change> <Change>Restored Fallback capabilities #9016</Change>
<Change>Implemented isConnectionActive() added in PersistenceBackend</Change>
</Changeset> </Changeset>
<Changeset component="org.gcube.data-publishing.document-store-lib-accounting-service.1-0-0" date="${buildDate}"> <Changeset component="org.gcube.data-publishing.document-store-lib-accounting-service.1-0-0" date="${buildDate}">
<Change>First Release</Change> <Change>First Release</Change>

View File

@ -233,6 +233,8 @@ public class HTTPCall {
String res = result.toString(); String res = result.toString();
logger.trace("Server returned content : {}", res); logger.trace("Server returned content : {}", res);
connection.disconnect();
} }

View File

@ -3,18 +3,14 @@
*/ */
package org.gcube.documentstore.persistence; package org.gcube.documentstore.persistence;
import java.io.StringWriter; import java.util.Arrays;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
import javax.xml.bind.JAXBContext;
import org.gcube.common.resources.gcore.GCoreEndpoint; import org.gcube.common.resources.gcore.GCoreEndpoint;
import org.gcube.documentstore.persistence.HTTPCall.HTTPMETHOD; import org.gcube.documentstore.persistence.HTTPCall.HTTPMETHOD;
import org.gcube.documentstore.records.DSMapper; import org.gcube.documentstore.records.DSMapper;
import org.gcube.documentstore.records.Record; import org.gcube.documentstore.records.Record;
import org.gcube.documentstore.records.SerializableList;
import org.gcube.resources.discovery.client.queries.api.SimpleQuery; import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
import org.gcube.resources.discovery.icclient.ICFactory; import org.gcube.resources.discovery.icclient.ICFactory;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -84,31 +80,18 @@ public class PersistenceAccountingService extends PersistenceBackend {
*/ */
@Override @Override
protected void reallyAccount(Record record) throws Exception { protected void reallyAccount(Record record) throws Exception {
String marshalledRecord = DSMapper.marshal(record); String marshalledRecord = DSMapper.marshal(record);
logger.trace("Goign to persist {} {}", Record.class.getSimpleName(), marshalledRecord); logger.trace("Going to persist {} {}", Record.class.getSimpleName(), marshalledRecord);
httpCall.call(PATH_SERVICE_INSERT_ACCOUNTING, HTTPMETHOD.POST, marshalledRecord, HTTPCall.APPLICATION_JSON_CHARSET_UTF_8); httpCall.call(PATH_SERVICE_INSERT_ACCOUNTING, HTTPMETHOD.POST, marshalledRecord, HTTPCall.APPLICATION_JSON_CHARSET_UTF_8);
} }
@Override @Override
protected void accountWithFallback(Record... records) throws Exception { protected void accountWithFallback(Record... records) throws Exception {
try { try {
List<String> valuesList = new ArrayList<String>(); List<Record> list = Arrays.asList(records);
for (Record record : records) { String body = DSMapper.marshal(list);
valuesList.add(DSMapper.marshal(record));
}
SerializableList<String> list = new SerializableList<String>(valuesList);
JAXBContext contextRecord = JAXBContext.newInstance(SerializableList.class);
StringWriter writer = new StringWriter();
contextRecord.createMarshaller().marshal(list, writer);
String body = writer.toString();
logger.trace("Goign to persist {}s {}", Record.class.getSimpleName(), body);
logger.trace("Going to persist {}s {}", Record.class.getSimpleName(), body);
httpCall.call(PATH_SERVICE_INSERTS_ACCOUNTING, HTTPMETHOD.POST, body, HTTPCall.APPLICATION_XML_CHARSET_UTF_8); httpCall.call(PATH_SERVICE_INSERTS_ACCOUNTING, HTTPMETHOD.POST, body, HTTPCall.APPLICATION_XML_CHARSET_UTF_8);
} catch (Exception e) { } catch (Exception e) {
@ -127,6 +110,14 @@ public class PersistenceAccountingService extends PersistenceBackend {
protected void closeConnection() throws Exception {} protected void closeConnection() throws Exception {}
@Override @Override
protected void closeAndClean() throws Exception {} public boolean isConnectionActive() throws Exception {
return true;
}
@Override
protected void clean() throws Exception {
// TODO Auto-generated method stub
}
} }

View File

@ -3,9 +3,15 @@
*/ */
package org.gcube.documentstore.persistence; package org.gcube.documentstore.persistence;
import java.util.Arrays;
import java.util.List;
import org.gcube.documentstore.records.DSMapper;
import org.gcube.documentstore.records.Record; import org.gcube.documentstore.records.Record;
import org.gcube.documentstore.utility.TestUsageRecord; import org.gcube.documentstore.utility.TestUsageRecord;
import org.junit.Test; import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* @author Alessandro Pieve (ISTI - CNR) alessandro.pieve@isti.cnr.it * @author Alessandro Pieve (ISTI - CNR) alessandro.pieve@isti.cnr.it
@ -13,6 +19,8 @@ import org.junit.Test;
*/ */
public class PersistenceAccountingServiceTest extends ScopedTest { public class PersistenceAccountingServiceTest extends ScopedTest {
private static final Logger logger = LoggerFactory.getLogger(PersistenceAccountingServiceTest.class);
@Test @Test
public void testSingleInsertService() throws Exception { public void testSingleInsertService() throws Exception {
String context = ScopedTest.getCurrentContext(); String context = ScopedTest.getCurrentContext();
@ -34,11 +42,17 @@ public class PersistenceAccountingServiceTest extends ScopedTest {
persistenceBackend = PersistenceBackendFactory.discoverPersistenceBackend(context, persistenceBackend = PersistenceBackendFactory.discoverPersistenceBackend(context,
(FallbackPersistenceBackend) persistenceBackend); (FallbackPersistenceBackend) persistenceBackend);
int count = 10; int count = 2;
Record[] records = new Record[count]; Record[] records = new Record[count];
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
records[i] = TestUsageRecord.createTestServiceUsageRecord(); records[i] = TestUsageRecord.createTestServiceUsageRecord();
} }
List<Record> recordList = Arrays.asList(records);
String ret = DSMapper.marshal(recordList);
logger.debug(ret);
persistenceBackend.accountWithFallback(records); persistenceBackend.accountWithFallback(records);
} }
@ -113,7 +127,8 @@ public class PersistenceAccountingServiceTest extends ScopedTest {
@Test @Test
public void testConfiguration() throws Exception { public void testConfiguration() throws Exception {
PersistenceBackendConfiguration persitenceConfiguration = PersistenceBackendConfiguration.getInstance(PersistenceAccountingService.class); PersistenceBackendConfiguration persitenceConfiguration = PersistenceBackendConfiguration
.getInstance(PersistenceAccountingService.class);
PersistenceAccountingService accountingService = new PersistenceAccountingService(); PersistenceAccountingService accountingService = new PersistenceAccountingService();
accountingService.prepareConnection(persitenceConfiguration); accountingService.prepareConnection(persitenceConfiguration);
} }

View File

@ -233,7 +233,7 @@ public class TestUsageRecord {
inputParameters.put(TEST_NESTED_MAP, parameter); inputParameters.put(TEST_NESTED_MAP, parameter);
usageRecord.setInputParameters(inputParameters); //usageRecord.setInputParameters(inputParameters);
} catch (InvalidValueException e) { } catch (InvalidValueException e) {
logger.error(" ------ You SHOULD NOT SEE THIS MESSAGE. Error Creating a test Usage Record", e); logger.error(" ------ You SHOULD NOT SEE THIS MESSAGE. Error Creating a test Usage Record", e);