001/* 002 * Copyright 2010 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.ametys.site; 018 019import java.io.IOException; 020 021import org.apache.cocoon.ProcessingException; 022import org.apache.cocoon.environment.ObjectModelHelper; 023import org.apache.cocoon.environment.Request; 024import org.apache.cocoon.reading.ServiceableReader; 025import org.apache.http.Header; 026import org.apache.http.HttpEntity; 027import org.apache.http.HttpResponse; 028import org.apache.http.impl.client.CloseableHttpClient; 029import org.apache.http.util.EntityUtils; 030import org.xml.sax.SAXException; 031 032/** 033 * Writes the CMS data direct to the client. 034 */ 035public class CMSResponseReader extends ServiceableReader 036{ 037 @Override 038 public void generate() throws IOException, SAXException, ProcessingException 039 { 040 Request request = ObjectModelHelper.getRequest(objectModel); 041 042 HttpResponse cmsResponse = (HttpResponse) request.getAttribute("cms-response"); 043 HttpEntity entity = cmsResponse.getEntity(); 044 045 if (entity != null) 046 { 047 entity.writeTo(out); 048 EntityUtils.consume(entity); 049 } 050 051 out.flush(); 052 } 053 054 @Override 055 public String getMimeType() 056 { 057 Request request = ObjectModelHelper.getRequest(objectModel); 058 059 HttpResponse cmsResponse = (HttpResponse) request.getAttribute("cms-response"); 060 Header contentType = cmsResponse.getFirstHeader("Content-Type"); 061 return contentType != null ? contentType.getValue() : null; 062// HttpEntity entity = cmsResponse.getEntity(); 063// return entity.getContentType() != null ? entity.getContentType().getValue() : null; 064 } 065 066 @Override 067 public void recycle() 068 { 069 Request request = ObjectModelHelper.getRequest(objectModel); 070 071 try (CloseableHttpClient httpClient = (CloseableHttpClient) request.getAttribute("http-client")) 072 { 073 httpClient.close(); 074 } 075 catch (IOException e) 076 { 077 // Nothing 078 } 079 080 super.recycle(); 081 } 082}