Skip to content

Instantly share code, notes, and snippets.

@mborodov
Created September 5, 2014 06:26
Show Gist options
  • Save mborodov/68f054aa3324518fb3d1 to your computer and use it in GitHub Desktop.
Save mborodov/68f054aa3324518fb3d1 to your computer and use it in GitHub Desktop.
package place;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import javax.jdo.PersistenceManager;
import javax.jdo.annotations.Transactional;
import javax.servlet.http.*;
import com.google.gson.Gson;
import engine.PMF;
import engine.PlacerServlet;
import engine.ServerError;
// Сервлет стрима
@SuppressWarnings("serial")
public class StreamServlet extends PlacerServlet {
//Создаем объект логгер
private static final Logger log = Logger.getLogger(StreamServlet.class.getName());
@Transactional
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
// Ловим исключение долгой работы сервлета ( > 30 секунд)
try {
final long timeStart = System.currentTimeMillis();
long timeCurrent = 0;
// Принимаем URL параметры
String latStr = req.getParameter("latitude");
String lonStr = req.getParameter("longitude");
String offset = req.getParameter("offset");
timeCurrent = System.currentTimeMillis() - timeStart;
log.info("Start:" + timeCurrent);
// Проверяем на ошибку 101
List<String> paramsError = new ArrayList<String>();
boolean error101Flag = false;
if(latStr == null || latStr.isEmpty() ){
paramsError.add("latitude");
error101Flag = true;
}
if(lonStr == null || lonStr.isEmpty() ){
paramsError.add("longitude");
error101Flag = true;
}
if(offset == null || offset.isEmpty() ){
paramsError.add("offset");
error101Flag = true;
}
if(error101Flag){
ServerError error101 = new ServerError(101, paramsError);
resp.getWriter().println(makeError(new Gson().toJson(error101)));
return;
}
timeCurrent = System.currentTimeMillis() - timeStart;
log.info("NearbyBegin:" + timeCurrent);
// Открываем PM
PersistenceManager pm = PMF.get().getPersistenceManager();
// Получаем места "рядом"
List<Place> resultNearby = PlaceHelper.getNearbyPlaces(Double.valueOf(latStr),Double.valueOf(lonStr),Integer.valueOf(offset));
timeCurrent = System.currentTimeMillis()- timeStart;
log.info("NearbyReady:" + timeCurrent);
// Создаем объект сессии, достаем от туда переменную login, если сессии нет то логин = null
HttpSession placerSession = req.getSession(false);
String sessionLogin = placerSession != null ? (String)placerSession.getAttribute("login"): null;
timeCurrent = System.currentTimeMillis() - timeStart;
log.info("getPlaces:" + timeCurrent);
// Если места рядом найдены, то подгатавливаем к выдачи места, иначе результат - пустой массив
List<Place> resultPlaces = new ArrayList<Place>();
if (resultNearby != null) {
resultPlaces = PlaceHelper.preparePlacesForClient(resultNearby, sessionLogin, pm);
}
// if (!resultPlaces.isEmpty()) {
// // Удаляем забаненые места
// resultPlaces = PlaceHelper.removeBannedPlaces(resultPlaces,sessionLogin);
// }
pm.close();
timeCurrent = System.currentTimeMillis() - timeStart;
log.info("resultPlaces:" + timeCurrent);
// Выводим ответ
resp.setContentType("text/plain");
resp.setCharacterEncoding("UTF8");
timeCurrent = System.currentTimeMillis() - timeStart;
log.info("encode:" + timeCurrent);
resp.getWriter().println(makeDoneArrayResponse(new Gson().toJson(resultPlaces)));
timeCurrent = System.currentTimeMillis() - timeStart;
log.info("finaltime:" + timeCurrent);
} catch (com.google.apphosting.api.DeadlineExceededException deadlineExceededException) {
// Пишем в лог ошибку DeadlineExceededException + логин юзера-инициатора ошибки
log.severe("DeadlineExceededException:" + getLoginFromRequest(req));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment