org.dataone.proto.trove.mn.service.v1.impl.MNStorageImpl.java Source code

Java tutorial

Introduction

Here is the source code for org.dataone.proto.trove.mn.service.v1.impl.MNStorageImpl.java

Source

/*
 * This work was created by participants in the DataONE project, and is
 * jointly copyrighted by participating institutions in DataONE. For
 * more information on DataONE, see our web site at http://dataone.org.
 * 
 * Copyright 2014
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package org.dataone.proto.trove.mn.service.v1.impl;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.inject.Named;
import org.apache.log4j.Logger;
import org.dataone.proto.trove.util.IndexMap;
import org.dataone.service.exceptions.IdentifierNotUnique;
import org.dataone.service.exceptions.InsufficientResources;
import org.dataone.service.exceptions.InvalidRequest;
import org.dataone.service.exceptions.InvalidSystemMetadata;
import org.dataone.service.exceptions.InvalidToken;
import org.dataone.service.exceptions.NotAuthorized;
import org.dataone.service.exceptions.NotFound;
import org.dataone.service.exceptions.NotImplemented;
import org.dataone.service.exceptions.ServiceFailure;
import org.dataone.service.exceptions.UnsupportedType;
import org.dataone.service.mn.tier3.v1.MNStorage;
import org.dataone.service.types.v1.Identifier;
import org.dataone.service.types.v1.Session;
import org.dataone.service.types.v1.SystemMetadata;
import org.dataone.service.util.EncodingUtilities;
import org.dataone.service.util.TypeMarshaller;
import org.jibx.runtime.JiBXException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

/**
 *
 * @author waltz
 */

@Named("mnStorageService")
public class MNStorageImpl implements MNStorage {
    Logger logger = Logger.getLogger(MNStorageImpl.class.getName());

    private String dataoneCacheDirectory;

    final static int SIZE = 16384;

    @Override
    public Identifier create(Identifier pid, InputStream object, SystemMetadata sysmeta)
            throws InvalidToken, ServiceFailure, NotAuthorized, IdentifierNotUnique, UnsupportedType,
            InsufficientResources, InvalidSystemMetadata, NotImplemented, InvalidRequest {
        try {
            File systemMetadata = new File(dataoneCacheDirectory + File.separator + "meta" + File.separator
                    + EncodingUtilities.encodeUrlPathSegment(pid.getValue()));
            TypeMarshaller.marshalTypeToFile(sysmeta, systemMetadata.getAbsolutePath());

            if (systemMetadata.exists()) {
                systemMetadata.setLastModified(sysmeta.getDateSysMetadataModified().getTime());
            } else {
                throw new ServiceFailure("1190", "SystemMetadata not found on FileSystem after create");
            }
            File objectFile = new File(dataoneCacheDirectory + File.separator + "object" + File.separator
                    + EncodingUtilities.encodeUrlPathSegment(pid.getValue()));
            if (!objectFile.exists() && objectFile.createNewFile()) {
                objectFile.setReadable(true);
                objectFile.setWritable(true);
                objectFile.setExecutable(false);
            }

            if (object != null) {
                FileOutputStream objectFileOutputStream = new FileOutputStream(objectFile);
                BufferedInputStream inputStream = new BufferedInputStream(object);
                byte[] barray = new byte[SIZE];
                int nRead = 0;

                while ((nRead = inputStream.read(barray, 0, SIZE)) != -1) {
                    objectFileOutputStream.write(barray, 0, nRead);
                }
                objectFileOutputStream.flush();
                objectFileOutputStream.close();
                inputStream.close();
            }
        } catch (FileNotFoundException ex) {
            throw new ServiceFailure("1190", ex.getCause().toString() + ":" + ex.getMessage());
        } catch (IOException ex) {
            throw new ServiceFailure("1190", ex.getMessage());
        } catch (JiBXException ex) {
            throw new InvalidSystemMetadata("1180", ex.getMessage());
        }
        return pid;
    }

    @Override
    public Identifier update(Session cert, Identifier pid, InputStream object, Identifier newPid,
            SystemMetadata sysmeta)
            throws InvalidToken, ServiceFailure, NotAuthorized, IdentifierNotUnique, UnsupportedType,
            InsufficientResources, NotFound, InvalidSystemMetadata, NotImplemented, InvalidRequest {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Identifier create(Session cert, Identifier pid, InputStream object, SystemMetadata sysmeta)
            throws IdentifierNotUnique, InsufficientResources, InvalidRequest, InvalidSystemMetadata, InvalidToken,
            NotAuthorized, NotImplemented, ServiceFailure, UnsupportedType {
        return this.create(pid, object, sysmeta);
    }

    @Override
    public Identifier update(Identifier pid, InputStream object, Identifier newPid, SystemMetadata sysmeta)
            throws IdentifierNotUnique, InsufficientResources, InvalidRequest, InvalidSystemMetadata, InvalidToken,
            NotAuthorized, NotImplemented, ServiceFailure, UnsupportedType, NotFound {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Identifier delete(Identifier pid)
            throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, NotImplemented {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Identifier archive(Identifier pid)
            throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, NotImplemented {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Identifier generateIdentifier(String scheme, String fragment)
            throws InvalidToken, ServiceFailure, NotAuthorized, NotImplemented, InvalidRequest {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Identifier delete(Session session, Identifier pid)
            throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, NotImplemented {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Identifier archive(Session session, Identifier pid)
            throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, NotImplemented {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Identifier generateIdentifier(Session session, String scheme, String fragment)
            throws InvalidToken, ServiceFailure, NotAuthorized, NotImplemented, InvalidRequest {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}