Java tutorial
/* * Copyright Tek Counsel LLC 2013 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or * implied. See the License for the specific language governing * permissions and limitations under the License. */ package com.tc.webshell.servlets; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.logging.Level; import java.util.logging.Logger; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import lotus.domino.Database; import lotus.domino.Document; import lotus.domino.Item; import lotus.domino.NotesException; import org.apache.commons.io.IOUtils; import com.ibm.domino.osgi.core.context.ContextInfo; import com.tc.utils.StrUtils; import com.tc.webshell.core.MapperFactory; import com.tc.webshell.core.Prompt; import com.tc.webshell.core.WebshellConstants; /** * * @author mwambler */ public class RestSimple extends WebshellServlet { private static final long serialVersionUID = 8097550209995147520L; private static final Logger logger = Logger.getLogger(RestSimple.class.getName()); /** * Processes requests for both HTTP * <code>GET</code> and * <code>POST</code> methods. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("application/json;charset=UTF-8"); try { Database db = ContextInfo.getUserDatabase(); String json = null; if ("POST".equals(request.getMethod())) { json = IOUtils.toString(request.getInputStream()); } else if ("GET".equals(request.getMethod())) { json = request.getParameter(WebshellConstants.PARAM_JSON); } else { json = IOUtils.toString(request.getInputStream()); } if (StrUtils.isNull(json)) { Prompt error = new Prompt(); error.setMessage("No json data found"); error.setTitle("ERROR"); json = MapperFactory.mapper().writeValueAsString(error); this.compressResponse(request, response, json); return; } Document doc = new DocFactory().buildDocument(request, db, json); String message = doc.getItemValueString("Form") + " created successfully"; Prompt prompt = new Prompt(); prompt.setMessage(message); prompt.setTitle("info"); prompt.addProperty(WebshellConstants.PARAM_NOTEID, doc.getNoteID()); prompt.addProperty(WebshellConstants.PARAM_UNID, doc.getUniversalID()); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd G 'at' HH:mm:ss z"); String strdate = formatter.format(doc.getCreated().toJavaDate()); prompt.addProperty(WebshellConstants.PARAM_CREATED, strdate); //lets add all the fields from the doc as a property for (Object o : doc.getItems()) { Item item = (Item) o; prompt.addProperty(item.getName(), item.getText()); } json = MapperFactory.mapper().writeValueAsString(prompt); this.compressResponse(request, response, json); doc.recycle(); } catch (NotesException e) { logger.log(Level.SEVERE, null, e); } } // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> /** * Handles the HTTP * <code>GET</code> method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Handles the HTTP * <code>POST</code> method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Returns a short description of the servlet. * * @return a String containing servlet description */ @Override public String getServletInfo() { return "Short description"; }// </editor-fold> }