/*
* Copyright (C) 2009 Rafael Fernandes
*
* 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.phonebooksharing.services.servlets;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import com.phonebooksharing.services.ejb3.facade.ContactFacade;
/**
* Servlet implementation class FriendServlet
*/
public class ContactServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ContactServlet() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter pw = response.getWriter();
response.setContentType("text/html");
pw.print("<html><head/><body><h1>Welcome to Phonebook Sharing Services!</h1><br/><h3>You should be posting here!</h3></body></html>");
}
@EJB
private ContactFacade contactFacade;
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
ObjectInputStream ois = new ObjectInputStream(request.getInputStream());
final String ac = request.getParameter("ac");
if ("b".equalsIgnoreCase(ac)) {// contact browser
final String authItem = (String) ois.readObject();
final int page = new Integer(request.getParameter("p"));
final int pageSize = new Integer(request.getParameter("ps"));
final long contactID = new Long(request.getParameter("c"));
ObjectOutputStream oos = new ObjectOutputStream(response.getOutputStream());
oos.writeObject(contactFacade.browseContacts(authItem, contactID, page, pageSize));
} else if ("gb".equalsIgnoreCase(ac)) {// group browsing
final String authItem = (String) ois.readObject();
ObjectOutputStream oos = new ObjectOutputStream(response.getOutputStream());
oos.writeObject(contactFacade.getGroups4Browsing(authItem));
} else if ("fb".equalsIgnoreCase(ac)) {// favorite browsing
final String authItem = (String) ois.readObject();
ObjectOutputStream oos = new ObjectOutputStream(response.getOutputStream());
oos.writeObject(contactFacade.getFavorites4Browsing(authItem));
} else if ("cl".equalsIgnoreCase(ac)) {// contact list
final String authItem = (String) ois.readObject();
final String accuracy = request.getParameter("a");
ObjectOutputStream oos = new ObjectOutputStream(response.getOutputStream());
if(StringUtils.isNotBlank(accuracy) && new Integer(accuracy) > 0) {//change it to StringUtils
oos.writeObject(contactFacade.getContactList(authItem, new Integer(accuracy)));
} else {
oos.writeObject(contactFacade.getContactList(authItem));
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
|