Package net.ajiaojr.spadger.server.servlet

Source Code of net.ajiaojr.spadger.server.servlet.LandingServlet$PageEmbedableSpadgerUser

/*
* Spadger - an open source discussion forum system.
*
* Copyright (C) 2011 The Spadger Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
*
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package net.ajiaojr.spadger.server.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Type;

import javax.inject.Singleton;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.ajiaojr.spadger.server.entity.SpadgerConfig;
import net.ajiaojr.spadger.server.entity.SpadgerUser;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.appengine.api.datastore.Text;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.annotations.Expose;

/**
* The landing servlet. This servlet redirects MSIE 6/7/8 users to the IE Notes
* page and mobile users to the mobile page.
*
* @author The Spadger Team
*/
@Singleton
public class LandingServlet extends HttpServlet {
  public class PageEmbedableSpadgerUser {
    @Expose
    private final String email;
    @Expose
    private final String id;
    @Expose
    private final String nickname;

    public PageEmbedableSpadgerUser(SpadgerUser spadgerUser) {
      this.email = spadgerUser.getEmail();
      this.id = spadgerUser.getEmail();
      this.nickname = spadgerUser.getNickname();
    }
  }

  /**
   *
   */
  private static final long serialVersionUID = 8708215123025910717L;

  public static final Gson GSON = new GsonBuilder()
      .excludeFieldsWithoutExposeAnnotation()
      .registerTypeAdapter(Text.class, new JsonSerializer<Text>() {

        @Override
        public JsonElement serialize(Text arg0, Type arg1,
            JsonSerializationContext arg2) {
          return new JsonPrimitive(arg0.getValue());
        }
      }).create();

  private static final Logger LOGGER = LoggerFactory
      .getLogger(LandingServlet.class);

  /*
   * (non-Javadoc)
   *
   * @see
   * javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
   * javax.servlet.http.HttpServletResponse)
   */
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {

    String userAgent = req.getHeader("User-Agent");
    LOGGER.trace("User-Agent for request is {}", userAgent);
    resp.setContentType("text/html;charset=UTF-8");
    resp.setCharacterEncoding("UTF-8");
    if (userAgent.contains("MSIE")) {
      resp.sendRedirect("http://code.google.com/p/spadger/wiki/IENotes");
    } else {
      PrintWriter out = resp.getWriter();
      out.append(
          "<!DOCTYPE html><meta charset=utf-8><meta name=gwt:property content=\"locale=")
          .append(req.getLocale().toString())
          .append("\"><title>Spadger</title><script src=")
          .append(req.getContextPath())
          .append("/s/s.nocache.js></script><body>\n");
      out.flush();

      // Setup the user information.
      SpadgerConfig config = SpadgerConfig.getInstance();
      out.append("<script>var Spadger={");
      if (req.getUserPrincipal() != null) {
        SpadgerUser userInfo = SpadgerUser.findSpadgerUser(UserServiceFactory
            .getUserService().getCurrentUser().getEmail());
        out.append("\"user\":")
            .append(GSON.toJson(new PageEmbedableSpadgerUser(userInfo)))
            .append(",");
      }
      out.append("\"system\":").append(GSON.toJson(config))
          .append("};</script>\n");
      out.flush();

      out.append("<script>var a=((\"https:\"==document.location.protocol)?\"https://ssl.\":\"http://www.\");document.write(unescape(\"%3Cscript src='\"+a+\"google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E\"));</script><script>try{var b=_gat._getTracker(\"UA-11829249-2\");b._trackPageview();}catch(e){}</script>");
    }
  }

  /*
   * (non-Javadoc)
   *
   * @see
   * javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest
   * , javax.servlet.http.HttpServletResponse)
   */
  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    doGet(req, resp);
  }
}
TOP

Related Classes of net.ajiaojr.spadger.server.servlet.LandingServlet$PageEmbedableSpadgerUser

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.