All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.geode.management.internal.web.http.converter.SerializableObjectHttpMessageConverter Maven / Gradle / Ivy

Go to download

Apache Geode provides a database-like consistency model, reliable transaction processing and a shared-nothing architecture to maintain very low latency performance with high concurrency processing

There is a newer version: 1.15.1
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for additional information regarding
 * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance with the License. You may obtain a
 * copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package org.apache.geode.management.internal.web.http.converter;

import java.io.IOException;
import java.io.Serializable;

import org.apache.geode.internal.lang.ObjectUtils;
import org.apache.geode.internal.util.IOUtils;

import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.AbstractHttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.util.StreamUtils;

/**
 * The SerializableObjectHttpMessageConverter class is a Spring HttpMessageConverter for converting
 * bytes streams to/from Serializable Objects.
 * 

* * @see java.io.Serializable * @see org.springframework.http.HttpInputMessage * @see org.springframework.http.HttpMessage * @see org.springframework.http.HttpOutputMessage * @see org.springframework.http.MediaType * @see org.springframework.http.converter.AbstractHttpMessageConverter * @since GemFire 8.0 */ @SuppressWarnings("unused") public class SerializableObjectHttpMessageConverter extends AbstractHttpMessageConverter { public SerializableObjectHttpMessageConverter() { super(MediaType.APPLICATION_OCTET_STREAM, MediaType.ALL); } /* * @Override public boolean canRead(final Class clazz, final MediaType mediaType) { return * canRead(mediaType); } */ @Override protected boolean supports(final Class type) { if (logger.isTraceEnabled()) { logger.trace(String.format("%1$s.supports(%2$s)", getClass().getName(), type.getName()), new Throwable()); } /* * logger.warn(String.format("%1$s.supports(%2$s)", getClass().getName(), type.getName())); * final StringWriter writer = new StringWriter(); new Throwable().printStackTrace(new * PrintWriter(writer)); logger.warn(writer.toString()); */ return (type != null && Serializable.class.isAssignableFrom(type)); } @Override protected Serializable readInternal(final Class type, final HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { try { return type.cast(IOUtils.deserializeObject(IOUtils.toByteArray(inputMessage.getBody()), ObjectUtils.defaultIfNull(type.getClassLoader(), getClass().getClassLoader()))); } catch (ClassNotFoundException e) { throw new HttpMessageNotReadableException( String.format("Unable to convert the HTTP message body into an Object of type (%1$s)", type.getName()), e); } } protected void setContentLength(final HttpMessage message, final byte[] messageBody) { message.getHeaders().setContentLength(messageBody.length); } @Override protected void writeInternal(final Serializable serializableObject, final HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { final byte[] messageBody = IOUtils.serializeObject(serializableObject); setContentLength(outputMessage, messageBody); StreamUtils.copy(messageBody, outputMessage.getBody()); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy