Skip to content

Commit

Permalink
move BinaryData to bus-common since it can then be used in BasicMessa…
Browse files Browse the repository at this point in the history
…ge parsing
  • Loading branch information
jmazzitelli committed Aug 3, 2015
1 parent f015860 commit d4fe7bf
Show file tree
Hide file tree
Showing 13 changed files with 21 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public static <T extends BasicMessage> T fromJSON(String json, Class<T> clazz) {
*
* Because of the way the JSON parser works, some extra data might have been read from the stream
* that wasn't part of the JSON message. In that case, a non-empty byte array containing the extra read
* data is returned in the map value.
* data is returned along with the input stream (that has presumably more binary data) in the returned map value.
*
* @param in input stream that has a JSON string at the head.
* @param clazz the class whose instance is represented by the JSON string
Expand All @@ -87,7 +87,7 @@ public static <T extends BasicMessage> T fromJSON(String json, Class<T> clazz) {
* in the stream. The value of the map is a byte array containing extra data that was read from
* the stream but not part of the JSON message.
*/
public static <T extends BasicMessage> Map<T, byte[]> fromJSON(InputStream in, Class<T> clazz) {
public static <T extends BasicMessage> Map<T, BinaryData> fromJSON(InputStream in, Class<T> clazz) {
final T obj;
final byte[] remainder;
try (JsonParser parser = new JsonFactory().configure(Feature.AUTO_CLOSE_SOURCE, false).createParser(in)) {
Expand All @@ -100,7 +100,7 @@ public static <T extends BasicMessage> Map<T, byte[]> fromJSON(InputStream in, C
} catch (Exception e) {
throw new IllegalArgumentException("Stream cannot be converted to JSON object of type [" + clazz + "]", e);
}
return Collections.singletonMap(obj, remainder);
return Collections.singletonMap(obj, new BinaryData(remainder, in));
}

private static Method findBuildObjectMapperForDeserializationMethod(Class<? extends BasicMessage> clazz) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hawkular.feedcomm.api;
package org.hawkular.bus.common;

import java.io.IOException;
import java.io.InputStream;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,18 @@ public void testReadFromInputStream() throws IOException {

ByteArrayInputStream in = new UncloseableByteArrayInputStream(jsonPlusExtra.getBytes());

Map<SimpleBasicMessage, byte[]> fromJsonMap = BasicMessage.fromJSON(in, SimpleBasicMessage.class);
Map<SimpleBasicMessage, BinaryData> fromJsonMap = BasicMessage.fromJSON(in, SimpleBasicMessage.class);
SimpleBasicMessage msg2 = fromJsonMap.keySet().iterator().next();
byte[] leftoverFromJsonParser = fromJsonMap.values().iterator().next();
BinaryData leftoverFromJsonParser = fromJsonMap.values().iterator().next();

Assert.assertEquals(msg.getMessage(), msg2.getMessage());
Assert.assertEquals(msg.getDetails(), msg2.getDetails());

// now make sure the stream still has our extra data that we can read now
byte[] leftoverFromStream = new byte[in.available()];
in.read(leftoverFromStream);
byte[] leftoverBytes = new byte[leftoverFromJsonParser.available()];
leftoverFromJsonParser.read(leftoverBytes);

String totalRemaining = new String(leftoverFromJsonParser, "UTF-8") + new String(leftoverFromStream, "UTF-8");
String totalRemaining = new String(leftoverBytes, "UTF-8");
Assert.assertEquals(extra.length(), totalRemaining.length());
Assert.assertEquals(extra, totalRemaining);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hawkular.feedcomm.api;
package org.hawkular.bus.common;

import java.io.ByteArrayInputStream;
import java.io.IOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.Map;

import org.hawkular.bus.common.BasicMessage;
import org.hawkular.bus.common.BinaryData;

/**
* Given the special syntax of "apiName=JSON" this will deserialize the JSON into the appropriate API POJO.
Expand Down Expand Up @@ -128,10 +128,8 @@ public <T extends BasicMessage> Map<T, BinaryData> deserialize(InputStream input
// We now have the name and the input stream is pointing at the JSON
try {
Class<T> pojo = (Class<T>) Class.forName(name);
Map<T, byte[]> results = BasicMessage.fromJSON(input, pojo);
return Collections.singletonMap(
results.keySet().iterator().next(),
new BinaryData(results.values().iterator().next(), input));
Map<T, BinaryData> results = BasicMessage.fromJSON(input, pojo);
return results;
} catch (Exception e) {
throw new RuntimeException("Cannot deserialize stream with object [" + name + "]", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Map;

import org.hawkular.bus.common.BasicMessage;
import org.hawkular.bus.common.BinaryData;
import org.junit.Assert;
import org.junit.Test;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package org.hawkular.feedcomm.ws.command;

import org.hawkular.bus.common.BasicMessage;
import org.hawkular.feedcomm.api.BinaryData;
import org.hawkular.bus.common.BinaryData;

/**
* An command that comes from a feed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import java.util.NoSuchElementException;
import java.util.Scanner;

import org.hawkular.feedcomm.api.BinaryData;
import org.hawkular.bus.common.BinaryData;
import org.hawkular.feedcomm.api.EchoRequest;
import org.hawkular.feedcomm.api.EchoResponse;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package org.hawkular.feedcomm.ws.command;

import org.hawkular.bus.common.BasicMessage;
import org.hawkular.feedcomm.api.BinaryData;
import org.hawkular.bus.common.BinaryData;
import org.hawkular.feedcomm.api.GenericErrorResponse;
import org.hawkular.feedcomm.ws.MsgLogger;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
import java.util.Map;

import org.hawkular.bus.common.BasicMessage;
import org.hawkular.bus.common.BinaryData;
import org.hawkular.bus.common.ConnectionContextFactory;
import org.hawkular.bus.common.Endpoint;
import org.hawkular.bus.common.MessageProcessor;
import org.hawkular.bus.common.producer.ProducerConnectionContext;
import org.hawkular.feedcomm.api.BinaryData;
import org.hawkular.feedcomm.api.ExecuteOperationResponse;
import org.hawkular.feedcomm.ws.Constants;
import org.hawkular.feedcomm.ws.MsgLogger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
import java.util.Collections;
import java.util.Map;

import org.hawkular.bus.common.BinaryData;
import org.hawkular.bus.common.ConnectionContextFactory;
import org.hawkular.bus.common.Endpoint;
import org.hawkular.bus.common.MessageId;
import org.hawkular.bus.common.MessageProcessor;
import org.hawkular.bus.common.producer.ProducerConnectionContext;
import org.hawkular.feedcomm.api.BinaryData;
import org.hawkular.feedcomm.api.ExecuteOperationRequest;
import org.hawkular.feedcomm.api.GenericSuccessResponse;
import org.hawkular.feedcomm.ws.Constants;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
import javax.websocket.server.ServerEndpoint;

import org.hawkular.bus.common.BasicMessage;
import org.hawkular.bus.common.BinaryData;
import org.hawkular.feedcomm.api.ApiDeserializer;
import org.hawkular.feedcomm.api.BinaryData;
import org.hawkular.feedcomm.api.GenericErrorResponseBuilder;
import org.hawkular.feedcomm.ws.Constants;
import org.hawkular.feedcomm.ws.MsgLogger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
import javax.websocket.server.ServerEndpoint;

import org.hawkular.bus.common.BasicMessage;
import org.hawkular.bus.common.BinaryData;
import org.hawkular.feedcomm.api.ApiDeserializer;
import org.hawkular.feedcomm.api.BinaryData;
import org.hawkular.feedcomm.api.GenericErrorResponseBuilder;
import org.hawkular.feedcomm.ws.Constants;
import org.hawkular.feedcomm.ws.MsgLogger;
Expand Down

0 comments on commit d4fe7bf

Please sign in to comment.