コード例 #1
0
  public static ContentData marshal(Object o, Environment env) {
    MarshallerWriteContext context = null;
    ContentData content = null;
    try {
      MarshallingConfigurationImpl marshallingConfigurationImpl = null;
      if (env != null) {
        marshallingConfigurationImpl =
            new MarshallingConfigurationImpl(
                (ObjectMarshallingStrategy[])
                    env.get(EnvironmentName.OBJECT_MARSHALLING_STRATEGIES),
                false,
                false);
      } else {
        marshallingConfigurationImpl =
            new MarshallingConfigurationImpl(
                new ObjectMarshallingStrategy[] {
                  new SerializablePlaceholderResolverStrategy(
                      ClassObjectMarshallingStrategyAcceptor.DEFAULT)
                },
                false,
                false);
      }
      ObjectMarshallingStrategyStore objectMarshallingStrategyStore =
          marshallingConfigurationImpl.getObjectMarshallingStrategyStore();
      ByteArrayOutputStream stream = new ByteArrayOutputStream();

      context =
          new MarshallerWriteContext(stream, null, null, null, objectMarshallingStrategyStore, env);
      Variable marshallVariable = null;
      if (o instanceof Map) {
        marshallVariable =
            ProtobufProcessMarshaller.marshallVariablesMap(context, (Map<String, Object>) o);
      } else {
        marshallVariable = ProtobufProcessMarshaller.marshallVariable(context, "results", o);
      }
      PersisterHelper.writeToStreamWithHeader(context, marshallVariable);

      context.close();

      byte[] toByteArray = stream.toByteArray();

      content = new ContentData();
      content.setContent(toByteArray);
      content.setType(o.getClass().getCanonicalName());
      content.setAccessType(AccessType.Inline);

    } catch (IOException ex) {
      ex.printStackTrace();
    }
    return content;
  }