public void writeResults( SlingHttpServletRequest request, JSONWriter write, Iterator<Result> results) throws JSONException { ExtendedJSONWriter exWriter = (ExtendedJSONWriter) write; Session session = StorageClientUtils.adaptToSession( request.getResourceResolver().adaptTo(javax.jcr.Session.class)); String currUser = request.getRemoteUser(); try { // write out the profile information for each result while (results.hasNext()) { Result result = results.next(); // start the object here so we can decorate with contact details write.object(); super.writeResult(request, write, result, true); // add contact information if appropriate String otherUser = String.valueOf(result.getFirstValue("path")); connMgr.writeConnectionInfo(exWriter, session, currUser, otherUser); write.endObject(); } } catch (StorageClientException e) { LOGGER.error(e.getMessage(), e); } catch (AccessDeniedException e) { LOGGER.error(e.getMessage(), e); } }
@Override protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException { // get current user String user = request.getRemoteUser(); if (user == null) { response.sendError( HttpServletResponse.SC_UNAUTHORIZED, "User must be logged in to check their status"); } LOGGER.info("GET to PresenceContactsServlet (" + user + ")"); response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); try { Writer writer = response.getWriter(); ExtendedJSONWriter output = new ExtendedJSONWriter(writer); // start JSON object output.object(); PresenceUtils.makePresenceJSON(output, user, presenceService, true); // add in the list of contacts info Session session = request.getResourceResolver().adaptTo(Session.class); List<String> userIds = connectionManager.getConnectedUsers(user, ConnectionState.ACCEPTED); output.key("contacts"); output.array(); for (String userId : userIds) { output.object(); // put in the basics PresenceUtils.makePresenceJSON(output, userId, presenceService, true); // add in the profile output.key("profile"); Authorizable au = PersonalUtils.getAuthorizable(session, userId); Node profileNode = (Node) session.getItem(PersonalUtils.getProfilePath(au)); ExtendedJSONWriter.writeNodeToWriter(output, profileNode); output.endObject(); } output.endArray(); // finish it output.endObject(); } catch (JSONException e) { LOGGER.error(e.getMessage(), e); response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage()); } catch (RepositoryException e) { LOGGER.error(e.getMessage(), e); response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage()); } return; }