com.turt2live.xmail.api.Attachments.java Source code

Java tutorial

Introduction

Here is the source code for com.turt2live.xmail.api.Attachments.java

Source

/*******************************************************************************
 * 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;

import com.turt2live.xmail.XMail;
import com.turt2live.xmail.mail.attachment.Attachment;
import com.turt2live.xmail.mail.attachment.UnknownAttachment;
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 layer before the AttachmentHandlerRegistry to build a pre-existing attachment. This is used
 * internally when fetching mail
 *
 * @author turt2live
 */
public class Attachments {

    /**
     * Attempts to get an attachment from a JSON string. This will throw an IllegalArgumentException if
     * the json string is null or invalid.
     *
     * @param json the JSON string
     *
     * @return the attachment, or an UnknownAttachment if no match
     */
    @SuppressWarnings("unchecked")
    public static Attachment getAttachment(String json) {
        if (json == null) {
            throw new IllegalArgumentException();
        }
        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<?, ?> json1 = (Map<?, ?>) parser.parse(json, containerFactory);
            Iterator<?> iter = json1.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>) json1;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            return null;
        }
        XMail plugin = XMail.getInstance();
        Attachment a = plugin.getMailServer().attemptAttachmentDecode(map);
        if (a != null) {
            return a;
        }
        a = plugin.getAttachmentHandlerRegistry().getAttachmentFromHandler(json);
        return a == null ? new UnknownAttachment(json) : a;
    }

}