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.httppart.*; 022import org.apache.juneau.internal.*; 023import org.apache.juneau.serializer.*; 024import org.apache.juneau.utils.*; 025 026/** 027 * HttpEntity for serializing POJOs as the body of HTTP requests. 028 * 029 * @deprecated Use {@link org.apache.juneau.http.SerializedHttpEntity}. 030 */ 031@Deprecated 032public final class RestRequestEntity extends BasicHttpEntity { 033 final Object output; 034 final Serializer serializer; 035 final HttpPartSchema schema; 036 byte[] outputBytes; 037 038 /** 039 * Constructor. 040 * 041 * @param input The POJO to serialize. Can also be a {@link Reader} or {@link InputStream}. 042 * @param serializer The serializer to use to serialize this response. 043 * @param schema The optional schema information about the serialized part. 044 */ 045 public RestRequestEntity(Object input, Serializer serializer, HttpPartSchema schema) { 046 this.output = input; 047 this.serializer = serializer; 048 this.schema = schema; 049 if (serializer != null && serializer.getResponseContentType() != null) 050 setContentType(new BasicHeader("Content-Type", serializer.getResponseContentType().toString())); 051 } 052 053 @Override /* BasicHttpEntity */ 054 public void writeTo(OutputStream os) throws IOException { 055 os = new NoCloseOutputStream(os); 056 if (output instanceof InputStream) { 057 IOPipe.create(output, os).run(); 058 } else if (output instanceof Reader) { 059 try (OutputStreamWriter osw = new OutputStreamWriter(os, UTF8)) { 060 IOPipe.create(output, osw).run(); 061 } 062 } else { 063 try { 064 if (serializer == null) { 065 // If no serializer specified, just close the stream. 066 os.close(); 067 } else { 068 SerializerSessionArgs sArgs = SerializerSessionArgs.create().schema(schema); 069 SerializerSession session = serializer.createSession(sArgs); 070 try (Closeable c = session.isWriterSerializer() ? new OutputStreamWriter(os, UTF8) : os) { 071 session.serialize(output, c); 072 } 073 } 074 } catch (SerializeException e) { 075 throw new org.apache.juneau.rest.client.RestCallException(e); 076 } 077 } 078 } 079 080 @Override /* BasicHttpEntity */ 081 public boolean isRepeatable() { 082 return true; 083 } 084 085 @Override /* BasicHttpEntity */ 086 public InputStream getContent() { 087 if (outputBytes == null) { 088 try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { 089 writeTo(baos); 090 outputBytes = baos.toByteArray(); 091 } catch (IOException e) { 092 throw new RuntimeException(e); 093 } 094 } 095 return new ByteArrayInputStream(outputBytes); 096 } 097}