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.response;
014
015import static org.apache.juneau.internal.StringUtils.*;
016
017import java.io.*;
018import java.util.*;
019
020import org.apache.juneau.*;
021import org.apache.juneau.http.*;
022import org.apache.juneau.rest.*;
023
024/**
025 * Response handler for {@link Writable} and {@link ReaderResource} objects.
026 * 
027 * <p>
028 * Uses the {@link Writable#writeTo(Writer)} method to send the contents to the
029 * {@link RestResponse#getNegotiatedWriter()} writer.
030 * 
031 * <h5 class='section'>See Also:</h5>
032 * <ul>
033 *    <li class='link'><a class="doclink" href="../../../../../overview-summary.html#juneau-rest-server.MethodReturnTypes">Overview &gt; juneau-rest-server &gt; Method Return Types</a>
034 * </ul>
035 */
036public final class StreamableHandler implements ResponseHandler {
037
038   @Override /* ResponseHandler */
039   public boolean handle(RestRequest req, RestResponse res, Object output) throws IOException, RestException {
040      if (output instanceof Streamable) {
041         if (output instanceof StreamResource) {
042            StreamResource r = (StreamResource)output;
043            MediaType mediaType = r.getMediaType();
044            if (mediaType != null)
045               res.setContentType(mediaType.toString());
046            for (Map.Entry<String,Object> h : r.getHeaders().entrySet())
047               res.setHeader(h.getKey(), asString(h.getValue()));
048         }
049         try (OutputStream os = res.getOutputStream()) {
050            ((Streamable)output).streamTo(os);
051         }
052         return true;
053      }
054      return false;
055   }
056}
057