本文整理汇总了Java中org.apache.sling.api.SlingHttpServletResponse.setStatus方法的典型用法代码示例。如果您正苦于以下问题:Java SlingHttpServletResponse.setStatus方法的具体用法?Java SlingHttpServletResponse.setStatus怎么用?Java SlingHttpServletResponse.setStatus使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.sling.api.SlingHttpServletResponse
的用法示例。
在下文中一共展示了SlingHttpServletResponse.setStatus方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: doPost
import org.apache.sling.api.SlingHttpServletResponse; //导入方法依赖的package包/类
@Override
protected void doPost(final SlingHttpServletRequest request, final SlingHttpServletResponse response)
throws ServletException, IOException {
try {
final Map<String, InputStream> files = Maps.newHashMap();
for (RequestParameter file : request.getRequestParameters("file")) {
files.put(file.getFileName(), file.getInputStream());
}
final List<Script> scripts = scriptStorage
.saveAll(files, isOverwrite(request), request.getResourceResolver());
if (shouldRedirect(request)) {
response.sendRedirect(getRedirectTo(request));
} else {
ServletUtils.writeJson(response, ScriptUtils.toJson(scripts));
}
} catch (RepositoryException e) {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
ServletUtils.writeJson(response, "Cannot save script in repository: " + e.getMessage());
}
}
示例2: removeSingleFile
import org.apache.sling.api.SlingHttpServletResponse; //导入方法依赖的package包/类
private void removeSingleFile(ResourceResolver resolver, SlingHttpServletResponse response,
String fileName) throws IOException {
if (StringUtils.isEmpty(fileName)) {
ServletUtils.writeMessage(response, "error", "File name to be removed cannot be empty");
} else {
final Script script = scriptFinder.find(fileName, resolver);
if (script == null) {
ServletUtils
.writeMessage(response, "error", String.format("Script not found: '%s'", fileName));
} else {
final String scriptPath = script.getPath();
try {
scriptStorage.remove(script, resolver);
ServletUtils.writeMessage(response, "success",
String.format("Script removed successfully: %s", scriptPath));
} catch (RepositoryException e) {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
ServletUtils.writeJson(response,
String.format("Cannot remove script: '%s'." + " Repository error: %s", scriptPath,
e.getMessage()));
}
}
}
}
示例3: doPost
import org.apache.sling.api.SlingHttpServletResponse; //导入方法依赖的package包/类
@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response)
throws ServletException, IOException {
final String content = request.getParameter("content");
if (StringUtils.isEmpty(content)) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
ServletUtils.writeMessage(response, "error", "Script content is required");
return;
}
try {
final Progress progress = scriptManager.evaluate(content, Mode.VALIDATION, request.getResourceResolver());
if (progress.isSuccess()) {
ServletUtils.writeMessage(response, "success", "Script passes validation");
} else {
final String message = progress.getLastError().getLastMessageText();
final Map<String, Object> context = new HashMap<>();
if (message != null) {
context.put("error", message);
}
ServletUtils.writeMessage(response, "error", "Script does not pass validation", context);
}
} catch (RepositoryException e) {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
ServletUtils.writeMessage(response, "error", String.format(
"Script' cannot be validated because of " + "repository error: %s", e.getMessage()));
}
}
示例4: doPost
import org.apache.sling.api.SlingHttpServletResponse; //导入方法依赖的package包/类
@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response)
throws ServletException, IOException {
final String all = request.getParameter("confirmation");
final String fileName = request.getParameter("file");
ResourceResolver resolver = request.getResourceResolver();
if (fileName != null) {
removeSingleFile(resolver, response, fileName);
} else if (all != null) {
removeAllFiles(resolver, response, all);
} else {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
ServletUtils.writeMessage(response, "error", "Invalid arguments specified");
}
}
示例5: removeAllFiles
import org.apache.sling.api.SlingHttpServletResponse; //导入方法依赖的package包/类
private void removeAllFiles(ResourceResolver resolver, SlingHttpServletResponse response, String all)
throws IOException {
if (!Boolean.parseBoolean(all)) {
ServletUtils.writeMessage(response, "error", "Remove all scripts is not confirmed");
} else {
final List<String> paths = new LinkedList<>();
final List<Script> scripts = scriptFinder.findAll(resolver);
try {
for (Script script : scripts) {
final String path = script.getPath();
scriptStorage.remove(script, resolver);
paths.add(path);
}
final Map<String, Object> context = new HashMap<>();
context.put("paths", paths);
ServletUtils.writeMessage(response, "success",
String.format("Scripts removed successfully, total: %d", scripts.size()), context);
} catch (RepositoryException e) {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
ServletUtils.writeJson(response,
"Cannot save remove all scripts. Repository error: " + e.getMessage());
}
}
}
示例6: doPost
import org.apache.sling.api.SlingHttpServletResponse; //导入方法依赖的package包/类
@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response)
throws ServletException, IOException {
final ResourceResolver resolver = request.getResourceResolver();
final String searchPath = request.getParameter("scriptPath");
final Script script = scriptFinder.find(searchPath, resolver);
if (script == null) {
ServletUtils.writeMessage(response, "error", "Script not found: " + searchPath);
return;
}
final ModifiableScript modifiableScript = new ModifiableScriptWrapper(resolver, script);
try {
final String executionMode = request.getParameter("executionMode");
if (executionMode != null) {
modifiableScript.setExecutionMode(ExecutionMode.valueOf(executionMode.toUpperCase()));
}
final String executionEnabled = request.getParameter("executionEnabled");
if (executionEnabled != null) {
modifiableScript.setExecutionEnabled(BooleanUtils.toBoolean(executionEnabled));
}
ServletUtils.writeMessage(response, "success", "Script configuration updated");
} catch (PersistenceException e) {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
ServletUtils
.writeMessage(response, "error", "Cannot update script configuration: " + e.getMessage());
}
}
示例7: doGet
import org.apache.sling.api.SlingHttpServletResponse; //导入方法依赖的package包/类
/**
* Return all comments on a GET request in order of newest to oldest.
*/
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
throws ServletException, IOException {
final PrintWriter writer = response.getWriter();
response.setCharacterEncoding(CharEncoding.UTF_8);
response.setContentType("application/json");
List<Resource> comments = commentService.getComments(request);
try {
JSONArray jsonArray = new JSONArray();
for (Resource comment : comments) {
final JSONObject json = new JSONObject();
final ValueMap properties = comment.getValueMap();
final Resource post = commentService.getParentPost(comment);
json.put(PublickConstants.COMMENT_PROPERTY_COMMENT,
properties.get(PublickConstants.COMMENT_PROPERTY_COMMENT, String.class));
json.put(JSON_ID, properties.get(JcrConstants.JCR_UUID, String.class));
json.put(JSON_EDITED, properties.get(PublickConstants.COMMENT_PROPERTY_EDITED, false));
json.put(JSON_REPLIES, commentService.numberOfReplies(comment));
json.put(JSON_SPAM, properties.get(PublickConstants.COMMENT_PROPERTY_SPAM, false));
json.put(JSON_POST, new JSONObject()
.put(JSON_POST_TEXT, post.getValueMap().get(PublickConstants.COMMENT_PROPERTY_TITLE, String.class))
.put(JSON_POST_LINK, linkRewriter.rewriteLink(post.getPath(), request.getServerName())));
jsonArray.put(json);
}
response.setStatus(SlingHttpServletResponse.SC_OK);
writer.write(jsonArray.toString());
} catch (JSONException e) {
LOGGER.error("Could not write JSON", e);
response.setStatus(SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
}
示例8: doGet
import org.apache.sling.api.SlingHttpServletResponse; //导入方法依赖的package包/类
/**
* Return all packages on a GET request in order of newest to oldest.
*/
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
throws ServletException, IOException {
final PrintWriter writer = response.getWriter();
response.setCharacterEncoding(CharEncoding.UTF_8);
response.setContentType("application/json");
List<JcrPackage> packages = packageService.getPackageList(request);
try {
JSONArray jsonArray = new JSONArray();
for (JcrPackage jcrPackage : packages) {
final JSONObject json = getJsonFromJcrPackage(jcrPackage);
jsonArray.put(json);
}
response.setStatus(SlingHttpServletResponse.SC_OK);
writer.write(jsonArray.toString());
} catch (JSONException | RepositoryException e) {
LOGGER.error("Could not write JSON", e);
response.setStatus(SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
}
示例9: doPost
import org.apache.sling.api.SlingHttpServletResponse; //导入方法依赖的package包/类
/**
* Save system properties on POST.
*
* @param request The Sling HTTP servlet request.
* @param response The Sling HTTP servlet response.
*/
@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response)
throws ServletException, IOException {
final PrintWriter writer = response.getWriter();
final boolean allowWrite = userService.isAuthorable(request.getResourceResolver().adaptTo(Session.class));
response.setCharacterEncoding(CharEncoding.UTF_8);
response.setContentType("application/json");
if (allowWrite) {
final String blogName = request.getParameter(BLOG_NAME_PROPERTY);
final boolean extensionlessUrls = Boolean.parseBoolean(request.getParameter(EXTENSIONLESS_URLS_PROPERTY));
final String tempDir = request.getParameter(TEMPORARY_DIRECTORY_PROPERTY);
final Map<String, Object> properties = new HashMap<String, Object>();
properties.put(SystemSettingsService.SYSTEM_BLOG_NAME, blogName);
properties.put(SystemSettingsService.SYSTEM_EXTENSIONLESS_URLS, extensionlessUrls);
properties.put(SystemSettingsService.SYSTEM_TEMPORARY_DIRECTORY, tempDir);
boolean result = systemSettingsService.setProperties(properties);
if (result) {
response.setStatus(SlingHttpServletResponse.SC_OK);
sendResponse(writer, "OK", "Settings successfully updated.");
} else {
response.setStatus(SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR);
sendResponse(writer, "Error", "Settings failed to update.");
}
} else {
response.setStatus(SlingHttpServletResponse.SC_FORBIDDEN);
sendResponse(writer, "Error", "Current user not authorized.");
}
}
示例10: doPost
import org.apache.sling.api.SlingHttpServletResponse; //导入方法依赖的package包/类
/**
* Handle POST request and send appropriate response after file
* is uploaded.
*/
@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response)
throws ServletException, IOException {
final String path = request.getParameter(PATH_REQUEST_PARAM);
String file = fileUploadService.uploadFile(request, path);
if (file != null) {
response.setStatus(response.SC_OK);
} else {
response.setStatus(response.SC_INTERNAL_SERVER_ERROR);
}
}
示例11: doPost
import org.apache.sling.api.SlingHttpServletResponse; //导入方法依赖的package包/类
/**
* Send email on post to servlet. The submission is verified through the reCAPTCHA
* service before submitting. Upon completion, a JSON response is returned.
*
* @param request The Sling HTTP servlet request
* @param response The Sling HTTP servlet response
* @see <a href="http://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-using-smtp-java.html">Sending an Email Through the Amazon SES SMTP Interface with Java</a>
*/
@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/json");
PrintWriter writer = response.getWriter();
boolean notRobot = recaptchaService.validate(request);
if (notRobot) {
final String submitterName = request.getParameter("name");
final String submitterEmail = request.getParameter("email");
final String submitterMessage = request.getParameter("message");
if (StringUtils.isNotEmpty(submitterName)
&& StringUtils.isNotEmpty(submitterName)
&& StringUtils.isNotEmpty(submitterName)) {
final String body = String.format(BODY, submitterName, submitterEmail, submitterMessage);
final boolean result = emailService.sendMail(SUBJECT, body);
if (result) {
response.setStatus(SlingHttpServletResponse.SC_OK);
sendResponse(writer, SlingHttpServletResponse.SC_OK, "Email was sent successfully.");
} else {
response.setStatus(SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR);
sendResponse(writer, SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Internal server error.");
}
} else {
response.setStatus(SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR);
sendResponse(writer, SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR,
"Submission was missing the message, name or email.");
}
} else {
response.setStatus(SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR);
sendResponse(writer, SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR,
"The reCAPTCHA service determined that you were a robot.");
}
}
示例12: activate
import org.apache.sling.api.SlingHttpServletResponse; //导入方法依赖的package包/类
@Override
public void activate() {
SlingHttpServletRequest request = getRequest();
SlingHttpServletResponse response = getResponse();
ResourceResolver resolver = getResourceResolver();
isAnonymous = "anonymous".equals(resolver.adaptTo(Session.class).getUserID());
message = (String) request.getAttribute(SlingConstants.ERROR_MESSAGE);
Integer scObject = (Integer) request.getAttribute(SlingConstants.ERROR_STATUS);
code = (scObject != null) ? scObject.intValue() : response.SC_INTERNAL_SERVER_ERROR;
if (message == null) {
message = statusToString(code);
}
// Print stack trace only if the user is not anonymous
if (!isAnonymous) {
if (request.getAttribute(SlingConstants.ERROR_EXCEPTION) instanceof Throwable) {
Throwable throwable = (Throwable) request.getAttribute(SlingConstants.ERROR_EXCEPTION);
printStackTrace(throwable);
}
}
response.setStatus(code);
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
}
示例13: doGet
import org.apache.sling.api.SlingHttpServletResponse; //导入方法依赖的package包/类
/**
* Add overrides for other SlingAllMethodsServlet here (doHead, doTrace, doPut, doDelete, etc.)
*/
@Override
protected final void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws
ServletException, IOException {
// Implement custom handling of GET requests
// This should be idempotent and not change underlying data in any meaningful way;
// To be safe, never modify data (add/update/delete) in the context of a GET request
// Set the response type; this might be JSON, etc.
// The repsonse type is usually closely correlated to the extension the servlet listens on
response.setContentType("text/html");
// Do some work
Resource resource = request.getResourceResolver().getResource("/content/world");
ValueMap properties = resource.adaptTo(ValueMap.class);
if (properties != null) {
// Writing HTML in servlets is usually inadvisable, and is better suited to be provided via a JSP/Sightly template
// This is just an example.
response.getWriter().write("<html><head></head><body>Hello "
+ properties.get("name", "World")
+ "!</body></html>");
// By Default the 200 HTTP Response status code is used; below explicitly sets it.
response.setStatus(SlingHttpServletResponse.SC_OK);
} else {
// Set HTTP Response Status code appropriately
response.setStatus(SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR);
response.getWriter().write("ERROR");
}
}
示例14: doPost
import org.apache.sling.api.SlingHttpServletResponse; //导入方法依赖的package包/类
@Override
protected final void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws
ServletException, IOException {
// Implement custom handling of POST requests
// Get Request parameter value
String newWorld = request.getParameter("world");
// Do some work
Resource resource = request.getResourceResolver().getResource("/content/world");
ModifiableValueMap properties = resource.adaptTo(ModifiableValueMap.class);
properties.put("name", newWorld);
resource.getResourceResolver().commit();
// Set the content type as needed
// The repsonse type is usually closely correlated to the extension the servlet listens on.
response.setContentType("application/json");
// When constructing a JSON response, leverage the Sling JSON Apis
JSONObject jsonResponse = new JSONObject();
try {
jsonResponse.put("success", true);
jsonResponse.put("new-world", newWorld);
// Write the JSON to the response
response.getWriter().write(jsonResponse.toString(2));
// Be default, a 200 HTTP Response Status code is used
} catch (JSONException e) {
log.error("Could not formulate JSON response");
// Servlet failures should always return an approriate HTTP Status code
response.setStatus(SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR);
// If you do not set your own HTML Response content, the OOTB HATEOS Response is used
response.getWriter().write("ERROR");
}
}
示例15: writeJsonResponse
import org.apache.sling.api.SlingHttpServletResponse; //导入方法依赖的package包/类
private void writeJsonResponse(SlingHttpServletResponse response, RestResponse restResponse) throws IOException {
response.setStatus(restResponse.getHttpStatus());
response.setContentType(restResponse.getContentType());
response.setCharacterEncoding(restResponse.getCharSet());
PrintWriter pw = response.getWriter();
pw.print(restResponse.getJson());
pw.close();
}