本文整理汇总了Java中org.apache.sling.api.SlingHttpServletResponse.getOutputStream方法的典型用法代码示例。如果您正苦于以下问题:Java SlingHttpServletResponse.getOutputStream方法的具体用法?Java SlingHttpServletResponse.getOutputStream怎么用?Java SlingHttpServletResponse.getOutputStream使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.sling.api.SlingHttpServletResponse
的用法示例。
在下文中一共展示了SlingHttpServletResponse.getOutputStream方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: writeResponse
import org.apache.sling.api.SlingHttpServletResponse; //导入方法依赖的package包/类
private static void writeResponse(SlingHttpServletResponse response, Rendition rendition) throws IOException {
InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = rendition.getStream();
outputStream = response.getOutputStream();
// NOTE: This does not matter if dispatcher serves image
// Dispatcher will use extension of file, it does not save HTTP Content-Type header
response.setContentType(rendition.getMimeType());
IOUtils.copy(inputStream, outputStream);
} finally {
if (inputStream != null) inputStream.close();
if (outputStream != null) outputStream.close();
}
}
示例2: doGet
import org.apache.sling.api.SlingHttpServletResponse; //导入方法依赖的package包/类
@Override
@SuppressWarnings("unchecked")
protected void doGet(final SlingHttpServletRequest request, final SlingHttpServletResponse response)
throws IOException, ServletException {
//TODO: put all of the logic in a context processor (need to fix templating support filter bug first)
String transformName = BLANK;
if (PathInfoUtil.getSuffixSegments(request).length == 2) {
String firstSuffixSegment = PathInfoUtil.getFirstSuffixSegment(request);
if (this.namedImageTransformers.keySet().contains(firstSuffixSegment)) {
transformName = firstSuffixSegment;
}
}
//Adds the asset binary to the inputStream
try {
Resource assetResource = getAssetResource(request);
if (DamUtil.isAsset(assetResource)) {
Binary binary;
String mimeType = BLANK;
Asset asset = DamUtil.resolveToAsset(assetResource);
Resource original = asset.getOriginal();
Node assetNode = original.adaptTo(Node.class);
if (assetNode.hasNode(JCR_CONTENT)) {
Node assetInfo = assetNode.getNode(JCR_CONTENT);
if (assetInfo.hasProperty(JCR_MIMETYPE)) {
mimeType = assetInfo.getProperty(JCR_MIMETYPE).getString();
}
if (StringUtils.isNotBlank(mimeType)) {
response.setContentType(mimeType);
}
binary = assetInfo.getProperty(JCR_DATA).getBinary();
InputStream inputStream = binary.getStream();
OutputStream outputStream = response.getOutputStream();
boolean shouldTransform = StringUtils.isNotBlank(transformName);
if (shouldTransform && ImageUtils.isImage(assetResource)) {
double quality = 1;
double maxGifQuality = 255;
// Transform the image
final Layer layer = new Layer(inputStream, new Dimension(maxWidth, maxHeight));
Layer newLayer = null;
try {
final NamedImageTransformer namedImageTransformer = this.namedImageTransformers.get(transformName);
newLayer = namedImageTransformer.transform(layer);
if (StringUtils.isBlank(mimeType) || !ImageIO.getImageWritersByMIMEType(mimeType).hasNext()) {
mimeType = getImageMimeType(layer, asset.getName());
response.setContentType(mimeType);
}
// For GIF images the colors will be reduced according to the quality argument.
if (StringUtils.equals(mimeType, GIF_MIME_TYPE)) {
quality = quality * maxGifQuality;
}
newLayer.write(mimeType, quality, outputStream);
} finally {
if (layer != null) {
layer.dispose();
}
if (newLayer != null) {
newLayer.dispose();
}
}
} else {
ByteStreams.copy(inputStream, outputStream);
}
response.flushBuffer();
outputStream.close();
}
}
} catch (RepositoryException repoException) {
LOGGER.error("Repository Exception. ", repoException);
}
}
示例3: doPost
import org.apache.sling.api.SlingHttpServletResponse; //导入方法依赖的package包/类
@Override
protected void doPost(final SlingHttpServletRequest request, final SlingHttpServletResponse response)
throws ServletException, IOException {
String fileName = request.getParameter("filename");
String content = request.getParameter("content");
if (fileName == null || fileName.length() == 0) {
LOGGER.error("Parameter fileName is required");
return;
}
response.setContentType("application/octet-stream"); // Your content type
response.setHeader("Content-Disposition",
"attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
InputStream input = IOUtils.toInputStream(content);
int read = 0;
byte[] bytes = new byte[BYTES_DOWNLOAD];
OutputStream os = response.getOutputStream();
while ((read = input.read(bytes)) != -1) {
os.write(bytes, 0, read);
}
input.close();
os.flush();
os.close();
}
示例4: doGet
import org.apache.sling.api.SlingHttpServletResponse; //导入方法依赖的package包/类
/**
* Add overrides for other SlingSafeMethodsServlet here (doGeneric, doHead, doOptions, doTrace) *
*/
@Override
protected final void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws
ServletException, IOException {
// Implement custom handling of GET requests
boolean responseIsText = true;
if (responseIsText) {
// Write a standard text/html response
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
response.getWriter().write("<html><body>Text to write to response</body></html>");
} else {
// Write some binary data to the response; Such as sending back an Image or PDF
InputStream input = new InputStream() {
@Override
public int read() throws IOException {
return 0;
}
};
response.setContentType(getServletContext().getMimeType(request.getPathInfo()));
OutputStream output = response.getOutputStream();
byte[] buffer = new byte[FOUR_KB];
for (int length = 0; (length = input.read(buffer)) > 0;) {
output.write(buffer, 0, length);
output.flush();
}
}
}
示例5: doGet
import org.apache.sling.api.SlingHttpServletResponse; //导入方法依赖的package包/类
@Override
protected void doGet(final SlingHttpServletRequest request, final SlingHttpServletResponse response)
throws ServletException, IOException {
String fileName = request.getParameter("filename");
String filePath = request.getParameter("filepath");
String mode = request.getParameter("mode");
try {
final ResourceResolver resourceResolver = request.getResourceResolver();
final Session session = resourceResolver.adaptTo(Session.class);
if (!("view").equals(mode)) {
response.setContentType("application/octet-stream"); // Your content type
response.setHeader("Content-Disposition",
"attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
}
String path = StringUtils.replace(filePath, "_jcr_content", "jcr:content");
Node jcrContent = session.getNode(path + "/jcr:content");
InputStream input = jcrContent.getProperty("jcr:data").getBinary().getStream();
session.save();
int read;
byte[] bytes = new byte[BYTES_DOWNLOAD];
OutputStream os = response.getOutputStream();
while ((read = input.read(bytes)) != -1) {
os.write(bytes, 0, read);
}
input.close();
os.flush();
os.close();
} catch (RepositoryException e) {
LOGGER.error(e.getMessage(), e);
response.sendRedirect("/etc/cqsm.html");
// response.sendError(500);
}
}