Exemple #1
0
  public <T> T call(
      String queue, long timeout, MediaType mediaType, Object payload, Class<T> responseClass)
      throws IOException, TimeoutException, InterruptedException {
    String encoding = MediaTypes.getCharset(mediaType);

    byte[] body = getBytes(mediaType, payload);

    AMQP.BasicProperties.Builder bpb = new AMQP.BasicProperties.Builder();
    if (encoding != null) {
      bpb.contentEncoding(encoding);
    }

    RpcResponse response = call(queue, timeout, bpb.build(), body);

    MediaType responseMediaType = MediaType.valueOf(response.getProperties().getContentType());
    if (MediaTypes.isError(responseMediaType)) {
      RpcError rpcError;
      try {
        rpcError = fromBytes(responseMediaType, response.getBody(), RpcError.class);
      } catch (IllegalArgumentException e) {
        throw new IOException("unknown remote error");
      }

      if (rpcError == null) {
        throw new IOException("unknown remote error");
      } else {
        throw new IOException(rpcError.error + "\n" + rpcError.trace);
      }

    } else {
      return fromBytes(responseMediaType, response.getBody(), responseClass);
    }
  }
Exemple #2
0
 /**
  * Get Content-Type type provided by the client.
  *
  * @param req Request
  * @return Media type
  * @throws IOException If fails
  */
 @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
 private static MediaTypes getType(final Request req) throws IOException {
   MediaTypes list = new MediaTypes();
   final Iterable<String> headers = new RqHeaders.Base(req).header("Content-Type");
   for (final String hdr : headers) {
     list = list.merge(new MediaTypes(hdr));
   }
   if (list.isEmpty()) {
     list = new MediaTypes("*/*");
   }
   return list;
 }
Exemple #3
0
 public void send(
     RouteSpec routeSpec, String routingKey, AMQP.BasicProperties properties, Object payload)
     throws IOException {
   Route route = routeSpec.declareOn(channel, this);
   route.publishOn(
       channel, routingKey, properties, toBytes(MediaTypes.valueOf(properties), payload));
 }
Exemple #4
0
 public void send(RouteSpec routeSpec, AMQP.BasicProperties properties, Object payload)
     throws IOException {
   send(
       routeSpec,
       "",
       properties,
       getBytes(
           MediaTypes.withCharset(properties.getContentType(), properties.getContentEncoding()),
           payload));
 }