You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by th...@apache.org on 2011/08/22 17:51:33 UTC

svn commit: r1160314 - /cocoon/cocoon3/trunk/cocoon-rest/src/main/java/org/apache/cocoon/rest/controller/response/BinaryResponse.java

Author: thorsten
Date: Mon Aug 22 15:51:33 2011
New Revision: 1160314

URL: http://svn.apache.org/viewvc?rev=1160314&view=rev
Log:
Adding generic binary REST Response. You can use it to return e.g. images with the REST controller.

Added:
    cocoon/cocoon3/trunk/cocoon-rest/src/main/java/org/apache/cocoon/rest/controller/response/BinaryResponse.java   (with props)

Added: cocoon/cocoon3/trunk/cocoon-rest/src/main/java/org/apache/cocoon/rest/controller/response/BinaryResponse.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-rest/src/main/java/org/apache/cocoon/rest/controller/response/BinaryResponse.java?rev=1160314&view=auto
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-rest/src/main/java/org/apache/cocoon/rest/controller/response/BinaryResponse.java (added)
+++ cocoon/cocoon3/trunk/cocoon-rest/src/main/java/org/apache/cocoon/rest/controller/response/BinaryResponse.java Mon Aug 22 15:51:33 2011
@@ -0,0 +1,58 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.rest.controller.response;
+
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.Map;
+
+import org.apache.commons.io.IOUtils;
+
+/**
+ * Simple wrapper to to write binary data response to different content types.
+ * e.g. new BinaryResponse(new ByteArrayInputStream(imageBytes), "image/jpeg"); or 
+ * return new BinaryResponse(new ByteArrayInputStream(imageBytes), "image/png");
+ */
+public class BinaryResponse implements RestResponse {
+    private String contentType;
+    private int statusCode = 200;
+    private InputStream stream;
+
+    public BinaryResponse(InputStream stream, String contentType) {
+        this.stream = stream;
+        this.contentType = contentType;
+    }
+
+    public BinaryResponse(InputStream stream, String contentType, int statusCode) {
+        this.stream = stream;
+        this.contentType = contentType;
+        this.statusCode = statusCode;
+    }
+
+    public void execute(OutputStream outputStream) throws Exception {
+        IOUtils.copy(stream, outputStream);
+    }
+
+    public RestResponseMetaData setup(Map<String, Object> inputParameters)
+            throws Exception {
+        RestResponseMetaData restResponseMetaData = new RestResponseMetaData();
+        restResponseMetaData.setStatusCode(statusCode);
+        restResponseMetaData.setContentType(contentType);
+        return restResponseMetaData;
+    }
+
+}

Propchange: cocoon/cocoon3/trunk/cocoon-rest/src/main/java/org/apache/cocoon/rest/controller/response/BinaryResponse.java
------------------------------------------------------------------------------
    svn:eol-style = native