Java tutorial
/* * Copyright 2017 Andree Yosua. * * 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 Servlet; import Model.DBAdmin; import Model.DirectoryAdmin; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import twitter4j.Twitter; import twitter4j.TwitterException; import twitter4j.auth.RequestToken; /** * * @author Andree Yosua */ public class CallbackServlet extends HttpServlet { /** * 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("text/html;charset=UTF-8"); Twitter twitter = (Twitter) request.getSession().getAttribute("twitter"); RequestToken requestToken = (RequestToken) request.getSession().getAttribute("requestToken"); String verifier = request.getParameter("oauth_verifier"); try { twitter.getOAuthAccessToken(requestToken, verifier); request.getSession().removeAttribute("requestToken"); String twitterIDString = Long.toString(twitter.getId()); twitter4j.User user = twitter.showUser(twitter.getId()); Model.User loggedUser = DBAdmin.getTwitterUser(twitterIDString); if (loggedUser == null) { String username = user.getName().replaceAll(" ", ""); if (DBAdmin.isUsernameTaken(username)) { int i = 1; while (DBAdmin.isUsernameTaken(username + i)) { i++; } username += i; } DBAdmin.registerTwitter(twitterIDString, username, "null", "", "player", user.getName(), "unaffliated"); DirectoryAdmin.prepNewUserDirectory(request, username); loggedUser = DBAdmin.getTwitterUser(twitterIDString); } request.getSession().setAttribute("loggedUser", loggedUser); } catch (IllegalStateException | TwitterException ex) { throw new ServletException(ex); } response.sendRedirect("main"); } // <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> }