/portal-impl/src/com/liferay/portlet/internal/MimeResponseImpl.java

https://github.com/kiyoshilee/liferay-portal · Java · 189 lines · 129 code · 43 blank · 17 comment · 14 complexity · 923c530ac5d1579851b506c59a5d3a36 MD5 · raw file

  1. /**
  2. * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. package com.liferay.portlet.internal;
  15. import com.liferay.portal.kernel.model.Portlet;
  16. import com.liferay.portal.kernel.portlet.LiferayWindowState;
  17. import com.liferay.portal.kernel.util.Validator;
  18. import java.io.IOException;
  19. import java.io.OutputStream;
  20. import java.io.PrintWriter;
  21. import java.util.Locale;
  22. import javax.portlet.CacheControl;
  23. import javax.portlet.MimeResponse;
  24. import javax.portlet.PortletRequest;
  25. import javax.portlet.WindowState;
  26. /**
  27. * @author Brian Wing Shun Chan
  28. * @author Shuyang Zhou
  29. */
  30. public abstract class MimeResponseImpl
  31. extends PortletResponseImpl implements MimeResponse {
  32. @Override
  33. public void flushBuffer() throws IOException {
  34. response.flushBuffer();
  35. _calledFlushBuffer = true;
  36. }
  37. @Override
  38. public int getBufferSize() {
  39. return response.getBufferSize();
  40. }
  41. @Override
  42. public CacheControl getCacheControl() {
  43. Portlet portlet = getPortlet();
  44. int expirationTime = 0;
  45. Integer expCache = portlet.getExpCache();
  46. if (expCache != null) {
  47. expirationTime = expCache;
  48. }
  49. return new CacheControlImpl(null, expirationTime, false, false, this);
  50. }
  51. @Override
  52. public String getCharacterEncoding() {
  53. return response.getCharacterEncoding();
  54. }
  55. @Override
  56. public String getContentType() {
  57. return _contentType;
  58. }
  59. @Override
  60. public Locale getLocale() {
  61. return portletRequestImpl.getLocale();
  62. }
  63. @Override
  64. public OutputStream getPortletOutputStream()
  65. throws IllegalStateException, IOException {
  66. if (_calledGetWriter) {
  67. throw new IllegalStateException(
  68. "Unable to obtain OutputStream because Writer is already in " +
  69. "use");
  70. }
  71. if (_contentType == null) {
  72. setContentType(portletRequestImpl.getResponseContentType());
  73. }
  74. _calledGetPortletOutputStream = true;
  75. return response.getOutputStream();
  76. }
  77. @Override
  78. public PrintWriter getWriter() throws IllegalStateException, IOException {
  79. if (_calledGetPortletOutputStream) {
  80. throw new IllegalStateException(
  81. "Unable to obtain Writer because OutputStream is already in " +
  82. "use");
  83. }
  84. if (_contentType == null) {
  85. setContentType(portletRequestImpl.getResponseContentType());
  86. }
  87. _calledGetWriter = true;
  88. return response.getWriter();
  89. }
  90. public boolean isCalledFlushBuffer() {
  91. return _calledFlushBuffer;
  92. }
  93. public boolean isCalledGetPortletOutputStream() {
  94. return _calledGetPortletOutputStream;
  95. }
  96. public boolean isCalledGetWriter() {
  97. return _calledGetWriter;
  98. }
  99. @Override
  100. public boolean isCommitted() {
  101. return response.isCommitted();
  102. }
  103. @Override
  104. public void reset() {
  105. if (_calledFlushBuffer) {
  106. throw new IllegalStateException(
  107. "Unable to reset a buffer that has been flushed");
  108. }
  109. }
  110. @Override
  111. public void resetBuffer() {
  112. if (_calledFlushBuffer) {
  113. throw new IllegalStateException(
  114. "Unable to reset a buffer that has been flushed");
  115. }
  116. response.resetBuffer();
  117. }
  118. @Override
  119. public void setBufferSize(int bufferSize) {
  120. response.setBufferSize(bufferSize);
  121. }
  122. @Override
  123. public void setContentType(String contentType) {
  124. if (_calledGetPortletOutputStream || _calledGetWriter) {
  125. return;
  126. }
  127. if (Validator.isNull(contentType)) {
  128. throw new IllegalArgumentException("Content type is null");
  129. }
  130. String lifecycle = getLifecycle();
  131. WindowState windowState = portletRequestImpl.getWindowState();
  132. if (!contentType.startsWith(
  133. portletRequestImpl.getResponseContentType()) &&
  134. !lifecycle.equals(PortletRequest.RESOURCE_PHASE) &&
  135. !windowState.equals(LiferayWindowState.EXCLUSIVE)) {
  136. throw new IllegalArgumentException(
  137. contentType + " is an unsupported content type");
  138. }
  139. _contentType = contentType;
  140. response.setContentType(contentType);
  141. }
  142. private boolean _calledFlushBuffer;
  143. private boolean _calledGetPortletOutputStream;
  144. private boolean _calledGetWriter;
  145. private String _contentType;
  146. }