001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.rest.client;
014
015import static org.apache.juneau.internal.IOUtils.*;
016
017import java.io.*;
018
019import org.apache.http.entity.*;
020import org.apache.http.message.*;
021import org.apache.juneau.internal.*;
022import org.apache.juneau.serializer.*;
023import org.apache.juneau.utils.*;
024
025/**
026 * HttpEntity for serializing POJOs as the body of HTTP requests.
027 */
028public final class RestRequestEntity extends BasicHttpEntity {
029   final Object output;
030   final Serializer serializer;
031   byte[] outputBytes;
032
033   /**
034    * Constructor.
035    * 
036    * @param input The POJO to serialize.  Can also be a {@link Reader} or {@link InputStream}.
037    * @param serializer The serializer to use to serialize this response.
038    */
039   public RestRequestEntity(Object input, Serializer serializer) {
040      this.output = input;
041      this.serializer = serializer;
042      if (serializer != null && serializer.getResponseContentType() != null)
043         setContentType(new BasicHeader("Content-Type", serializer.getResponseContentType().toString()));
044   }
045
046   @Override /* BasicHttpEntity */
047   public void writeTo(OutputStream os) throws IOException {
048      os = new NoCloseOutputStream(os);
049      if (output instanceof InputStream) {
050         IOPipe.create(output, os).run();
051      } else if (output instanceof Reader) {
052         try (OutputStreamWriter osw = new OutputStreamWriter(os, UTF8)) {
053            IOPipe.create(output, osw).run();
054         }
055      } else {
056         try {
057            if (serializer == null) {
058               // If no serializer specified, just close the stream.
059               os.close();
060            } else {
061               SerializerSession session = serializer.createSession();
062               try (Closeable c = session.isWriterSerializer() ? new OutputStreamWriter(os, UTF8) : os) {
063                  session.serialize(output, c);
064               }
065            }
066         } catch (SerializeException e) {
067            throw new org.apache.juneau.rest.client.RestCallException(e);
068         }
069      }
070   }
071
072   @Override /* BasicHttpEntity */
073   public boolean isRepeatable() {
074      return true;
075   }
076
077   @Override /* BasicHttpEntity */
078   public InputStream getContent() {
079      if (outputBytes == null) {
080         try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
081            writeTo(baos);
082            outputBytes = baos.toByteArray();
083         } catch (IOException e) {
084            throw new RuntimeException(e);
085         }
086      }
087      return new ByteArrayInputStream(outputBytes);
088   }
089}