Esempio n. 1
0
 public Object doTransform(Object src, String encoding) throws TransformerException {
   byte[] buf;
   if (src instanceof String) {
     buf = src.toString().getBytes();
   } else if (src instanceof InputStream) {
     InputStream input = (InputStream) src;
     try {
       buf = IOUtils.toByteArray(input);
     } finally {
       IOUtils.closeQuietly(input);
     }
   } else {
     buf = (byte[]) src;
   }
   try {
     byte[] result = getTransformedBytes(buf);
     if (getReturnClass().equals(String.class)) {
       if (encoding != null) {
         try {
           return new String(result, encoding);
         } catch (UnsupportedEncodingException ex) {
           return new String(result);
         }
       } else {
         return new String(result);
       }
     } else {
       return result;
     }
   } catch (CryptoFailureException e) {
     throw new TransformerException(this, e);
   }
 }
Esempio n. 2
0
 public byte[] getPayloadForSerialization() throws Exception {
   if (message instanceof InputStream) {
     // message is an InputStream when the HTTP method was POST
     return IOUtils.toByteArray((InputStream) message);
   } else if (message instanceof Serializable) {
     // message is a String when the HTTP method was GET
     return SerializationUtils.serialize((Serializable) message);
   } else {
     throw new NotSerializableException(
         "Don't know how to serialize payload of type " + message.getClass().getName());
   }
 }
  protected <T> T deserializeJsonToEntity(
      final TypeReference<T> responseType, final MuleMessage response) throws MuleException {
    try {
      T entity;

      if (logger.isDebugEnabled()) {
        response.setPayload(IOUtils.toByteArray((InputStream) response.getPayload()));
        entity = OBJECT_MAPPER.<T>readValue((byte[]) response.getPayload(), responseType);
      } else {
        entity = OBJECT_MAPPER.<T>readValue((InputStream) response.getPayload(), responseType);
      }

      return entity;
    } catch (final IOException ioe) {
      throw new DefaultMuleException(
          "Failed to deserialize to: "
              + responseType.getType()
              + " from: "
              + renderMessageAsString(response),
          ioe);
    }
  }
Esempio n. 4
0
    protected MessageAdapter buildStandardAdapter(final HttpRequest request, final Map headers)
        throws MuleException, TransformerException, IOException {
      final RequestLine requestLine = request.getRequestLine();

      sendExpect100(headers, requestLine);

      Object body = request.getBody();

      // If http method is GET we use the request uri as the payload.
      if (body == null) {
        body = requestLine.getUri();
      } else {
        // If we are running async we need to read stream into a byte[].
        // Passing along the InputStream doesn't work because the
        // HttpConnection gets closed and closes the InputStream, often
        // before it can be read.
        if (!endpoint.isSynchronous()) {
          logger.debug("Reading HTTP POST InputStream into byte[] for asynchronous messaging.");
          body = IOUtils.toByteArray((InputStream) body);
        }
      }

      return connector.getMessageAdapter(new Object[] {body, headers});
    }