Serializer and Deserializer for java.sql.Blob for Jackson
parent
57a6f73e9b
commit
f8056cb421
|
|
@ -38,10 +38,12 @@ import javax.persistence.Table;
|
|||
import org.hibernate.envers.Audited;
|
||||
import org.hibernate.envers.NotAudited;
|
||||
import org.libreccm.core.UnexpectedErrorException;
|
||||
import org.libreccm.imexport.BlobJsonDeserializer;
|
||||
import org.libreccm.imexport.BlobJsonSerializer;
|
||||
import org.libreccm.jpa.utils.MimeTypeConverter;
|
||||
import org.libreccm.l10n.LocalizedString;
|
||||
import org.libreccm.ui.admin.imexport.MimeTypeJsonDeserializer;
|
||||
import org.libreccm.ui.admin.imexport.MimeTypeJsonSerializer;
|
||||
import org.libreccm.imexport.MimeTypeJsonDeserializer;
|
||||
import org.libreccm.imexport.MimeTypeJsonSerializer;
|
||||
import org.librecms.contentsection.privileges.AssetPrivileges;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
|
@ -161,6 +163,8 @@ public class BinaryAsset extends Asset implements Serializable {
|
|||
@Lob
|
||||
@Basic(fetch = FetchType.LAZY)
|
||||
@NotAudited // Workaround until bug in Hibernate is resolved: https://hibernate.atlassian.net/browse/HHH-14725
|
||||
@JsonSerialize(using = BlobJsonSerializer.class)
|
||||
@JsonDeserialize(using = BlobJsonDeserializer.class)
|
||||
private Blob data;
|
||||
|
||||
@Column(name = "DATA_SIZE")
|
||||
|
|
|
|||
|
|
@ -0,0 +1,127 @@
|
|||
package org.libreccm.imexport;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||
import org.hibernate.engine.jdbc.BlobProxy;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.sql.Blob;
|
||||
|
||||
import static java.lang.System.out;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
|
||||
*/
|
||||
public class BlobJsonDeserializer extends JsonDeserializer<Blob> {
|
||||
|
||||
@Override
|
||||
public Blob deserialize(
|
||||
final JsonParser parser,
|
||||
final DeserializationContext ctxt
|
||||
) throws IOException, JsonProcessingException {
|
||||
final Path tmpFilePath = Files.createTempFile("upload", "import");
|
||||
try(OutputStream outputStream = Files.newOutputStream(tmpFilePath)) {
|
||||
parser.readBinaryValue(outputStream);
|
||||
final Blob data = BlobProxy.generateProxy(
|
||||
new UploadInputStream(tmpFilePath),
|
||||
-1
|
||||
);
|
||||
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
private class UploadInputStream extends InputStream {
|
||||
|
||||
private final Path tmpFilePath;
|
||||
|
||||
private InputStream inputStream;
|
||||
|
||||
public UploadInputStream(final Path tmpFilePath) {
|
||||
this.tmpFilePath = tmpFilePath;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int available() throws IOException {
|
||||
openNewInputStreamIfNecessary();
|
||||
return inputStream.available();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
if (inputStream != null) {
|
||||
inputStream.close();
|
||||
inputStream = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mark(final int readLimit) {
|
||||
try {
|
||||
openNewInputStreamIfNecessary();
|
||||
} catch (IOException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
inputStream.mark(readLimit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean markSupported() {
|
||||
try {
|
||||
openNewInputStreamIfNecessary();
|
||||
} catch (IOException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
return inputStream.markSupported();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read() throws IOException {
|
||||
openNewInputStreamIfNecessary();
|
||||
return inputStream.read();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read(final byte[] data) throws IOException {
|
||||
openNewInputStreamIfNecessary();
|
||||
return inputStream.read(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read(final byte[] data, final int offset, final int length)
|
||||
throws IOException {
|
||||
openNewInputStreamIfNecessary();
|
||||
return inputStream.read(data, offset, length);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reset() throws IOException {
|
||||
if (inputStream == null) {
|
||||
openNewInputStreamIfNecessary();
|
||||
} else {
|
||||
inputStream.reset();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public long skip(long nBytes) throws IOException {
|
||||
openNewInputStreamIfNecessary();
|
||||
return inputStream.skip(nBytes);
|
||||
}
|
||||
|
||||
private void openNewInputStreamIfNecessary() throws IOException {
|
||||
if (inputStream == null) {
|
||||
inputStream = Files.newInputStream(tmpFilePath);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
package org.libreccm.imexport;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.Blob;
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
|
||||
*/
|
||||
public class BlobJsonSerializer extends JsonSerializer<Blob> {
|
||||
|
||||
@Override
|
||||
public void serialize(
|
||||
final Blob value,
|
||||
final JsonGenerator generator,
|
||||
final SerializerProvider serializers
|
||||
) throws IOException {
|
||||
try {
|
||||
generator.writeFieldName("data");
|
||||
generator.writeBinary(value.getBinaryStream(), -1);
|
||||
} catch (SQLException ex) {
|
||||
throw new IOException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,10 +1,9 @@
|
|||
package org.libreccm.ui.admin.imexport;
|
||||
package org.libreccm.imexport;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package org.libreccm.ui.admin.imexport;
|
||||
package org.libreccm.imexport;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
Loading…
Reference in New Issue