Java tutorial
/******************************************************************************* * Copyright (C) 2014 Travis Ralston (turt2live) * * 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 * (at your option) 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 com.turt2live.xmail.api.mail; import com.turt2live.xmail.engine.ServerResponse; import com.turt2live.xmail.engine.request.Request; import com.turt2live.xmail.engine.request.Request.RequestError; import com.turt2live.xmail.engine.request.RequestCallback; import com.turt2live.xmail.mail.Mail; import org.json.simple.parser.ContainerFactory; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; import java.util.*; import java.util.Map.Entry; /** * Represents a mail waiting class. Because of how the plugin processes request this class is used * to send back an immediate response without having to circumvent the existing system * * @author turt2live */ public class MailWaiter extends RequestCallback { private boolean hasMail = false, error = false; private List<Mail> mail = new ArrayList<Mail>(); @SuppressWarnings("unchecked") @Override public void done(Request request, ServerResponse response) { for (String line : response.getLines()) { JSONParser parser = new JSONParser(); ContainerFactory containerFactory = new ContainerFactory() { @Override public List<Object> creatArrayContainer() { return new LinkedList<Object>(); } @Override public Map<String, Object> createObjectContainer() { return new LinkedHashMap<String, Object>(); } }; Map<String, Object> map = new HashMap<String, Object>(); try { Map<?, ?> json = (Map<?, ?>) parser.parse(line, containerFactory); Iterator<?> iter = json.entrySet().iterator(); // Type check while (iter.hasNext()) { Entry<?, ?> entry = (Entry<?, ?>) iter.next(); if (!(entry.getKey() instanceof String)) { throw new IllegalArgumentException("Not in <String, Object> format"); } } map = (Map<String, Object>) json; } catch (ParseException e) { e.printStackTrace(); return; } catch (IllegalArgumentException e) { e.printStackTrace(); return; } Object mails = map.get("mail"); if (mails != null && mails instanceof List) { List<?> list = (List<?>) mails; List<Map<?, ?>> maps = new ArrayList<Map<?, ?>>(); for (Object o : list) { if (o instanceof Map) { maps.add((Map<?, ?>) o); } } List<Map<String, Object>> revisedMaps = new ArrayList<Map<String, Object>>(); for (Map<?, ?> o : maps) { Map<String, Object> m2 = new HashMap<String, Object>(); for (Object k : o.keySet()) { if (k instanceof String) { Object obj = o.get(k); m2.put((String) k, obj); } } revisedMaps.add(m2); } for (Map<String, Object> map2 : revisedMaps) { Mail m3 = Mail.fromJSON(map2); if (m3 != null) { this.mail.add(m3); } } } } hasMail = true; } @Override public void error(Request request, String error, RequestError code) { this.error = true; } /** * Gets the list of mail. This can be empty but not null. * * @return the list of mail */ public List<Mail> getMail() { return mail; } /** * Determines if the waiter encountered an error * * @return true if there was an error, false otherwise */ public boolean hasError() { return error; } /** * Determines if this waiter has mail * * @return true if there is mail */ public boolean hasMail() { return hasMail; } @Override public void queued(Request request) { } }