/** {@inheritDoc} */ public void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException { try { Resource resource = request.getResource(); Node node = resource.adaptTo(Node.class); if (node == null) { response.sendError(HttpServletResponse.SC_NOT_FOUND); return; } Version version = versionService.saveNode(node, request.getRemoteUser()); response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); ExtendedJSONWriter write = new ExtendedJSONWriter(response.getWriter()); write.object(); write.key("versionName"); write.value(version.getName()); ExtendedJSONWriter.writeNodeContentsToWriter(write, version); write.endObject(); } catch (RepositoryException e) { LOGGER.info("Failed to save version ", e); response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage()); return; } catch (JSONException e) { LOGGER.info("Failed to save version ", e); response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage()); return; } }
private void writeProfileMap( javax.jcr.Session jcrSession, AuthorizableManager um, ExtendedJSONWriter writer, String user, boolean detailed) throws JSONException, AccessDeniedException, StorageClientException, RepositoryException { Authorizable au = um.findAuthorizable(user); if (au != null) { ValueMap profileMap; if (detailed) { profileMap = profileService.getProfileMap(au, jcrSession); } else { profileMap = new ValueMapDecorator(basicUserInfoService.getProperties(au)); } if (profileMap != null) { writer.valueMap(profileMap); } } else { writer.object(); writer.key("userid"); writer.value(user); writer.endObject(); } }
@Override protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException { try { Resource resource = request.getResource(); if (!resource.getPath().startsWith(SEARCH_PATH_PREFIX)) { response.sendError( HttpServletResponse.SC_FORBIDDEN, "Search templates can only be executed if they are located under " + SEARCH_PATH_PREFIX); return; } Node node = resource.adaptTo(Node.class); if (node != null && node.hasProperty(SAKAI_QUERY_TEMPLATE)) { // TODO: we might want to use this ? @SuppressWarnings("unused") boolean limitResults = true; if (node.hasProperty(SAKAI_LIMIT_RESULTS)) { limitResults = node.getProperty(SAKAI_LIMIT_RESULTS).getBoolean(); } // KERN-1147 Respond better when all parameters haven't been provided for a query Query query = null; try { query = processQuery(request, node); } catch (MissingParameterException e) { response.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage()); return; } long nitems = SolrSearchUtil.longRequestParameter( request, PARAMS_ITEMS_PER_PAGE, DEFAULT_PAGED_ITEMS); long page = SolrSearchUtil.longRequestParameter(request, PARAMS_PAGE, 0); // allow number of items to be specified in sakai:query-template-options if (query.getOptions().containsKey(PARAMS_ITEMS_PER_PAGE)) { nitems = Long.valueOf(query.getOptions().get(PARAMS_ITEMS_PER_PAGE)); } else { // add this to the options so that all queries are constrained to a limited // number of returns per page. query.getOptions().put(PARAMS_ITEMS_PER_PAGE, Long.toString(nitems)); } if (query.getOptions().containsKey(PARAMS_PAGE)) { page = Long.valueOf(query.getOptions().get(PARAMS_PAGE)); } else { // add this to the options so that all queries are constrained to a limited // number of returns per page. query.getOptions().put(PARAMS_PAGE, Long.toString(page)); } boolean useBatch = false; // Get the SolrSearchBatchResultProcessor searchBatchProcessor = defaultSearchBatchProcessor; if (node.hasProperty(SAKAI_BATCHRESULTPROCESSOR)) { searchBatchProcessor = batchProcessors.get(node.getProperty(SAKAI_BATCHRESULTPROCESSOR).getString()); useBatch = true; if (searchBatchProcessor == null) { searchBatchProcessor = defaultSearchBatchProcessor; } } SolrSearchResultProcessor searchProcessor = defaultSearchProcessor; if (node.hasProperty(SAKAI_RESULTPROCESSOR)) { searchProcessor = processors.get(node.getProperty(SAKAI_RESULTPROCESSOR).getString()); if (searchProcessor == null) { searchProcessor = defaultSearchProcessor; } } SolrSearchResultSet rs = null; try { // Prepare the result set. // This allows a processor to do other queries and manipulate the results. if (useBatch) { rs = searchBatchProcessor.getSearchResultSet(request, query); } else { rs = searchProcessor.getSearchResultSet(request, query); } } catch (SolrSearchException e) { response.sendError(e.getCode(), e.getMessage()); return; } response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); ExtendedJSONWriter write = new ExtendedJSONWriter(response.getWriter()); write.setTidy(isTidy(request)); write.object(); write.key(PARAMS_ITEMS_PER_PAGE); write.value(nitems); write.key(JSON_RESULTS); write.array(); Iterator<Result> iterator = rs.getResultSetIterator(); if (useBatch) { LOGGER.info("Using batch processor for results"); searchBatchProcessor.writeResults(request, write, iterator); } else { LOGGER.info("Using regular processor for results"); // We don't skip any rows ourselves here. // We expect a rowIterator coming from a resultset to be at the right place. for (long i = 0; i < nitems && iterator.hasNext(); i++) { // Get the next row. Result result = iterator.next(); // Write the result for this row. searchProcessor.writeResult(request, write, result); } } write.endArray(); // write the total out after processing the list to give the underlying iterator // a chance to walk the results then report how many there were. write.key(TOTAL); write.value(rs.getSize()); write.endObject(); } } catch (RepositoryException e) { LOGGER.error(e.getMessage(), e); response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage()); } catch (JSONException e) { LOGGER.error(e.getMessage(), e); response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage()); } }
/** * {@inheritDoc} * * @see * org.apache.sling.api.servlets.SlingSafeMethodsServlet#doGet(org.apache.sling.api.SlingHttpServletRequest, * org.apache.sling.api.SlingHttpServletResponse) */ @Override protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException { try { List<Content> contentList = null; RequestParameter rp = request.getRequestParameter("path"); ResourceResolver resourceResolver = request.getResourceResolver(); if (rp != null) { String contentPath = rp.getString("UTF-8"); if (contentPath.startsWith("/_groupa:")) { contentPath = contentPath.replaceFirst("/_groupa:", "/~"); } if (contentPath.endsWith("/")) { contentPath = contentPath.substring(0, contentPath.length() - 1); } Resource pagesResource = resourceResolver.getResource(contentPath); if (pagesResource != null) { contentList = getPageTree(pagesResource.adaptTo(Content.class)); ; } } response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); PrintWriter w = response.getWriter(); ExtendedJSONWriter writer = new ExtendedJSONWriter(w); writer.object(); // pages info int messageCount = 0; writer.key("items"); writer.value(255); writer.key("results"); writer.array(); if (contentList != null) { for (int i = 0; i < contentList.size(); i++) { Content page = contentList.get(i); writer.object(); writer.key("jcr:path"); writer.value( page.getPath() .replaceFirst( LitePersonalUtils.PATH_AUTHORIZABLE, LitePersonalUtils.PATH_RESOURCE_AUTHORIZABLE)); for (String messagePropKey : page.getProperties().keySet()) { writer.key(messagePropKey); writer.value(massageValue(messagePropKey, page.getProperty(messagePropKey))); } writer.endObject(); messageCount++; } } writer.endArray(); writer.key("total"); writer.value(messageCount); writer.endObject(); } catch (JSONException e) { LOG.error("Failed to create proper JSON response in /var/search/page", e); response.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Failed to create proper JSON response."); } }