001/*
002 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     bstefanescu
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.webengine.model.impl;
021
022import javax.ws.rs.core.MediaType;
023
024import org.nuxeo.common.xmap.annotation.XContent;
025import org.nuxeo.common.xmap.annotation.XNode;
026import org.nuxeo.common.xmap.annotation.XObject;
027
028/**
029 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
030 */
031@XObject("media-type")
032public class MediaTypeRef {
033
034    @XNode("@id")
035    public String id;
036
037    public String type;
038
039    public String subtype;
040
041    @XContent
042    public void setMimeType(String mimeType) {
043        mimeType = mimeType.trim().toLowerCase();
044        int p = mimeType.indexOf('/');
045        if (p > -1) {
046            type = mimeType.substring(0, p);
047            subtype = mimeType.substring(p + 1);
048            if (subtype.length() == 0 || subtype.equals("*")) {
049                subtype = "*";
050            }
051        } else {
052            type = mimeType;
053            subtype = "*";
054        }
055        if (type.length() == 0 || type.equals("*")) {
056            type = "*";
057        }
058    }
059
060    public String match(MediaType mt) {
061        if (type != "*" && !type.equals(mt.getType())) {
062            return null;
063        }
064        if (subtype != "*" && !subtype.equals(mt.getSubtype())) {
065            return null;
066        }
067        return id;
068    }
069
070}