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

com.arangodb.shaded.vertx.ext.web.codec.impl.BodyCodecImpl Maven / Gradle / Ivy

There is a newer version: 7.9.0
Show newest version
/*
 * Copyright (c) 2011-2013 The original author or authors
 * ------------------------------------------------------
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Apache License v2.0 which accompanies this distribution.
 *
 *     The Eclipse Public License is available at
 *     http://www.eclipse.org/legal/epl-v10.html
 *
 *     The Apache License v2.0 is available at
 *     http://www.opensource.org/licenses/apache2.0.php
 *
 * You may elect to redistribute this code under either of these licenses.
 */
package com.arangodb.shaded.vertx.ext.web.codec.impl;

import com.arangodb.shaded.vertx.core.AsyncResult;
import com.arangodb.shaded.vertx.core.Future;
import com.arangodb.shaded.vertx.core.Handler;
import com.arangodb.shaded.vertx.core.Promise;
import com.arangodb.shaded.vertx.core.buffer.Buffer;
import com.arangodb.shaded.vertx.core.json.DecodeException;
import com.arangodb.shaded.vertx.core.json.Json;
import com.arangodb.shaded.vertx.core.json.JsonArray;
import com.arangodb.shaded.vertx.core.json.JsonObject;
import com.arangodb.shaded.vertx.core.streams.WriteStream;
import com.arangodb.shaded.vertx.ext.web.codec.BodyCodec;
import com.arangodb.shaded.vertx.ext.web.codec.spi.BodyStream;

import java.util.function.Function;

/**
 * @author Julien Viet
 */
public class BodyCodecImpl implements BodyCodec {

  public static final Function VOID_DECODER = buff -> null;
  public static final Function UTF8_DECODER = Buffer::toString;
  public static final Function JSON_OBJECT_DECODER = buff -> {
    Object val = Json.decodeValue(buff);
    if (val == null) {
      return null;
    }
    if (val instanceof JsonObject) {
      return (JsonObject) val;
    }
    throw new DecodeException("Invalid Json Object decoded as " + val.getClass().getName());
  };
  public static final Function JSON_ARRAY_DECODER = buff -> {
    Object val = Json.decodeValue(buff);
    if (val == null) {
      return null;
    }
    if (val instanceof JsonArray) {
      return (JsonArray) val;
    }
    throw new DecodeException("Invalid Json Object decoded as " + val.getClass().getName());
  };
  public static final BodyCodec STRING = new BodyCodecImpl<>(UTF8_DECODER);
  public static final BodyCodec NONE = new BodyCodecImpl<>(VOID_DECODER);
  public static final BodyCodec BUFFER = new BodyCodecImpl<>(Function.identity());
  public static final BodyCodec JSON_OBJECT = new BodyCodecImpl<>(JSON_OBJECT_DECODER);
  public static final BodyCodec JSON_ARRAY = new BodyCodecImpl<>(JSON_ARRAY_DECODER);

  public static BodyCodecImpl string(String encoding) {
    return new BodyCodecImpl<>(buff -> buff.toString(encoding));
  }

  public static  BodyCodec json(Class type) {
    return new BodyCodecImpl<>(jsonDecoder(type));
  }

  public static  Function jsonDecoder(Class type) {
    return buff -> Json.decodeValue(buff, type);
  }

  private final Function decoder;

  public BodyCodecImpl(Function decoder) {
    this.decoder = decoder;
  }

  @Override
  public void create(Handler>> handler) {
    handler.handle(Future.succeededFuture(new BodyStream() {

      final Buffer buffer = Buffer.buffer();
      final Promise state = Promise.promise();

      @Override
      public void handle(Throwable cause) {
        state.tryFail(cause);
      }

      @Override
      public Future result() {
        return state.future();
      }

      @Override
      public WriteStream exceptionHandler(Handler handler) {
        return this;
      }

      @Override
      public void write(Buffer data, Handler> handler) {
        buffer.appendBuffer(data);
        handler.handle(Future.succeededFuture());
      }

      @Override
      public Future write(Buffer data) {
        buffer.appendBuffer(data);
        return Future.succeededFuture();
      }

      @Override
      public void end(Handler> handler) {
        if (!state.future().isComplete()) {
          T result;
          if (buffer.length() > 0) {
            try {
              result = decoder.apply(buffer);
            } catch (Throwable t) {
              state.fail(t);
              if (handler != null) {
                handler.handle(Future.failedFuture(t));
              }
              return;
            }
          } else {
            result = null;
          }
          state.complete(result);
          if (handler != null) {
            handler.handle(Future.succeededFuture());
          }
        }
      }

      @Override
      public WriteStream setWriteQueueMaxSize(int maxSize) {
        return this;
      }

      @Override
      public boolean writeQueueFull() {
        return false;
      }

      @Override
      public WriteStream drainHandler(Handler handler) {
        return this;
      }
    }));
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy