Java tutorial
/** * * ?? * Users * * ? * ? * * * Copyright (c) 2015 OpenID Foundation Japan. * This is released under the MIT License, see LICENSE file. */ package jp.or.openid.eiwg.scim.servlet; import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; import java.util.ArrayList; import java.util.LinkedHashMap; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import jp.or.openid.eiwg.constants.MessageConstants; import jp.or.openid.eiwg.scim.operation.Operation; import org.apache.commons.io.IOUtils; import com.fasterxml.jackson.core.JsonGenerationException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; /** * Servlet implementation class Users */ @WebServlet("/scim/Users/*") public class Users extends HttpServlet { /** * ? * (PATCH ?????? HttpServlet.service() ?) * * @param request * @param response ? * @throws ServletException * @throws IOException */ protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String method = request.getMethod(); if (method.equals("GET")) { doGet(request, response); } else if (method.equals("POST")) { doPost(request, response); } else if (method.equals("PUT")) { doPut(request, response); } else if (method.equals("PATCH")) { doPatch(request, response); } else if (method.equals("DELETE")) { doDelete(request, response); } else { this.errorResponse(response, HttpServletResponse.SC_FORBIDDEN, null, MessageConstants.ERROR_NOT_SUPPORT_OPERATION); } } /** * GET? * * @param request * @param response ? * @throws ServletException * @throws IOException */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // ? ServletContext context = getServletContext(); // ?? Operation op = new Operation(); boolean result = op.Authentication(context, request); if (!result) { // this.errorResponse(response, op.getErrorCode(), op.getErrorType(), op.getErrorMessage()); } else { // ? String targetId = request.getPathInfo(); String attributes = request.getParameter("attributes"); String filter = request.getParameter("filter"); String sortBy = request.getParameter("sortBy"); String sortOrder = request.getParameter("sortOrder"); String startIndex = request.getParameter("startIndex"); String count = request.getParameter("count"); if (targetId != null && !targetId.isEmpty()) { // ?'/'??? targetId = targetId.substring(1); } // ArrayList<LinkedHashMap<String, Object>> resultList = op.searchUserInfo(context, request, targetId, attributes, filter, sortBy, sortOrder, startIndex, count); if (resultList != null) { ObjectMapper mapper = new ObjectMapper(); StringWriter writer = new StringWriter(); // ?? if (targetId != null && !targetId.isEmpty()) { if (!resultList.isEmpty()) { LinkedHashMap<String, Object> resultObject = resultList.get(0); // javaJSON?? mapper.writeValue(writer, resultObject); response.setContentType("application/scim+json;charset=UTF-8"); response.setHeader("Location", request.getRequestURL().toString()); PrintWriter out = response.getWriter(); out.println(writer); } else { // id????????? this.errorResponse(response, HttpServletResponse.SC_NOT_FOUND, null, MessageConstants.ERROR_NOT_FOUND); } } else { // javaJSON?? mapper.writeValue(writer, resultList); String listResponse = "{\"schemas\":[\"urn:ietf:params:scim:api:messages:2.0:ListResponse\"],"; listResponse += "\"totalResults\":" + Integer.toString(resultList.size()); if (resultList.size() > 0) { listResponse += ",\"Resources\":"; listResponse += writer.toString(); } listResponse += "}"; response.setContentType("application/scim+json;charset=UTF-8"); PrintWriter out = response.getWriter(); out.println(listResponse); } } else { // this.errorResponse(response, op.getErrorCode(), op.getErrorType(), op.getErrorMessage()); } } } /** * POST? * * @param request * @param response ? * @throws ServletException * @throws IOException */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // ? ServletContext context = getServletContext(); // ?? Operation op = new Operation(); boolean result = op.Authentication(context, request); if (!result) { // errorResponse(response, op.getErrorCode(), op.getErrorType(), op.getErrorMessage()); } else { // ? String targetId = request.getPathInfo(); String attributes = request.getParameter("attributes"); if (targetId != null && !targetId.isEmpty()) { // ?'/'??? targetId = targetId.substring(1); } if (targetId == null || targetId.isEmpty()) { // POST(JSON)? request.setCharacterEncoding("UTF-8"); String body = IOUtils.toString(request.getReader()); // ? LinkedHashMap<String, Object> resultObject = op.createUserInfo(context, request, attributes, body); if (resultObject != null) { // javaJSON?? ObjectMapper mapper = new ObjectMapper(); StringWriter writer = new StringWriter(); mapper.writeValue(writer, resultObject); // Location?URL? String location = request.getScheme() + "://" + request.getServerName(); int serverPort = request.getServerPort(); if (serverPort != 80 && serverPort != 443) { location += ":" + Integer.toString(serverPort); } location += request.getContextPath(); location += "/scim/Users/"; if (resultObject.get("id") != null) { location += resultObject.get("id").toString(); } // ?? response.setStatus(HttpServletResponse.SC_CREATED); response.setContentType("application/scim+json;charset=UTF-8"); response.setHeader("Location", location); PrintWriter out = response.getWriter(); out.println(writer.toString()); } else { // errorResponse(response, op.getErrorCode(), op.getErrorType(), op.getErrorMessage()); } } else { errorResponse(response, HttpServletResponse.SC_BAD_REQUEST, null, MessageConstants.ERROR_NOT_SUPPORT_OPERATION); } } } /** * PUT? * * @param request * @param response ? * @throws ServletException * @throws IOException */ protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // ? ServletContext context = getServletContext(); // ?? Operation op = new Operation(); boolean result = op.Authentication(context, request); if (!result) { // errorResponse(response, op.getErrorCode(), op.getErrorType(), op.getErrorMessage()); } else { // ? String targetId = request.getPathInfo(); String attributes = request.getParameter("attributes"); if (targetId != null && !targetId.isEmpty()) { // ?'/'??? targetId = targetId.substring(1); } if (targetId != null && !targetId.isEmpty()) { // PUT(JSON)? request.setCharacterEncoding("UTF-8"); String body = IOUtils.toString(request.getReader()); // LinkedHashMap<String, Object> resultObject = op.updateUserInfo(context, request, targetId, attributes, body); if (resultObject != null) { // javaJSON?? ObjectMapper mapper = new ObjectMapper(); StringWriter writer = new StringWriter(); mapper.writeValue(writer, resultObject); // Location?URL? String location = request.getScheme() + "://" + request.getServerName(); int serverPort = request.getServerPort(); if (serverPort != 80 && serverPort != 443) { location += ":" + Integer.toString(serverPort); } location += request.getContextPath(); location += "/scim/Users/"; if (resultObject.get("id") != null) { location += resultObject.get("id").toString(); } // ?? response.setStatus(HttpServletResponse.SC_OK); response.setContentType("application/scim+json;charset=UTF-8"); response.setHeader("Location", location); PrintWriter out = response.getWriter(); out.println(writer.toString()); } else { // errorResponse(response, op.getErrorCode(), op.getErrorType(), op.getErrorMessage()); } } else { errorResponse(response, HttpServletResponse.SC_BAD_REQUEST, null, MessageConstants.ERROR_NOT_SUPPORT_OPERATION); } } } /** * PATCH? * * @param request * @param response ? * @throws ServletException * @throws IOException */ protected void doPatch(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // ? errorResponse(response, HttpServletResponse.SC_FORBIDDEN, null, MessageConstants.ERROR_NOT_SUPPORT_OPERATION); } /** * DELETE? * * @param request * @param response ? * @throws ServletException * @throws IOException */ protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // ? ServletContext context = getServletContext(); // ?? Operation op = new Operation(); boolean result = op.Authentication(context, request); if (!result) { // errorResponse(response, op.getErrorCode(), op.getErrorType(), op.getErrorMessage()); } else { // ? String targetId = request.getPathInfo(); if (targetId != null && !targetId.isEmpty()) { // ?'/'??? targetId = targetId.substring(1); } if (targetId != null && !targetId.isEmpty()) { // boolean deleteResult = op.deleteUserInfo(context, request, targetId); if (deleteResult) { response.setStatus(HttpServletResponse.SC_NO_CONTENT); } else { // errorResponse(response, op.getErrorCode(), op.getErrorType(), op.getErrorMessage()); } } else { errorResponse(response, HttpServletResponse.SC_BAD_REQUEST, null, MessageConstants.ERROR_NOT_SUPPORT_OPERATION); } } } /** * ? * * @param code HTTP * @param type * @param message */ private void errorResponse(HttpServletResponse response, int code, String type, String message) throws IOException { try { // ?? ObjectMapper mapper = new ObjectMapper(); ObjectNode rootNode = mapper.createObjectNode(); ArrayNode schemasArray = rootNode.putArray("schemas"); schemasArray.add("urn:ietf:params:scim:api:messages:2.0:Error"); if (type != null && !type.isEmpty()) { rootNode.put("scimType", type); } rootNode.put("detail", message); rootNode.put("status", code); response.setStatus(code); response.setContentType("application/scim+json;charset=UTF-8"); PrintWriter out = response.getWriter(); mapper.writeValue(out, rootNode); } catch (JsonGenerationException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } } }