001/*
002 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Antoine Taillefer <ataillefer@nuxeo.com>
018 */
019package org.nuxeo.drive.operations;
020
021import java.io.IOException;
022
023import javax.mail.internet.ContentType;
024import javax.mail.internet.ParseException;
025
026import org.apache.commons.lang.StringUtils;
027import org.nuxeo.ecm.core.api.Blob;
028import org.nuxeo.ecm.core.api.Blobs;
029
030/**
031 * Helper for Nuxeo Drive operations.
032 *
033 * @author Antoine Taillefer
034 */
035public final class NuxeoDriveOperationHelper {
036
037    private NuxeoDriveOperationHelper() {
038        // Helper class
039    }
040
041    public static void normalizeMimeTypeAndEncoding(Blob blob) throws ParseException {
042
043        String mimeType = blob.getMimeType();
044        if (!StringUtils.isEmpty(mimeType) && !"null".equals(mimeType)) {
045            ContentType contentType = new ContentType(mimeType);
046            blob.setMimeType(contentType.getBaseType());
047            if (StringUtils.isEmpty(blob.getEncoding())) {
048                String charset = contentType.getParameter("charset");
049                if (!StringUtils.isEmpty(charset)) {
050                    blob.setEncoding(charset);
051                }
052            }
053        }
054    }
055
056    /**
057     * @deprecated since 9.2, use {@link Blobs#createJSONBlobFromValueJackson1} directly
058     */
059    @Deprecated
060    public static Blob asJSONBlob(Object value) throws IOException {
061        return Blobs.createJSONBlobFromValueJackson1(value);
062    }
063
064}