org.dataone.proto.trove.mn.rest.v1.SystemMetadataController.java Source code

Java tutorial

Introduction

Here is the source code for org.dataone.proto.trove.mn.rest.v1.SystemMetadataController.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.rest.v1;

import java.io.UnsupportedEncodingException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.net.URLCodec;
import org.apache.log4j.Logger;

import org.dataone.proto.trove.mn.rest.base.AbstractWebController;
import org.dataone.service.exceptions.ServiceFailure;
import org.dataone.service.mn.tier1.v1.MNRead;
import org.dataone.service.types.v1.Checksum;

import org.dataone.service.types.v1.Identifier;
import org.dataone.service.types.v1.Session;
import org.dataone.service.types.v1.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import org.dataone.service.types.v1.SystemMetadata;

/**
 * READ ONLY of systemMetadata functions.
 *
 * @author waltz
 *
 */
@Controller
public class SystemMetadataController extends AbstractWebController {

    static Pattern checksumPattern = Pattern.compile("checksumAlgorithm\\=([^&]+)");
    Logger logger = Logger.getLogger(SystemMetadataController.class.getName());
    @Autowired
    @Qualifier("mnReadService")
    MNRead mnRead;
    private URLCodec urlCodec = new URLCodec();

    @RequestMapping(value = "/v1/meta/{pid}", method = RequestMethod.GET)
    public ModelAndView meta(HttpServletRequest request, HttpServletResponse response, @PathVariable String pid)
            throws Exception {
        debugRequest(request);
        Identifier id = new Identifier();
        try {
            id.setValue(urlCodec.decode(pid, "UTF-8"));
        } catch (DecoderException ex) {
            throw new ServiceFailure("20000", ex.getMessage());
        } catch (UnsupportedEncodingException ex) {
            throw new ServiceFailure("20001", ex.getMessage());
        }
        Session cert = new Session();

        SystemMetadata systemMetadata = mnRead.getSystemMetadata(cert, id);

        logger.debug("Meta: " + systemMetadata.getIdentifier());

        return new ModelAndView("xmlMetaViewResolver", "org.dataone.service.types.v1.SystemMetadata",
                systemMetadata);
    }

    @RequestMapping(value = "/v1/checksum/{pid}", method = RequestMethod.GET)
    public ModelAndView checksum(HttpServletRequest request, HttpServletResponse response, @PathVariable String pid)
            throws Exception {
        debugRequest(request);
        Identifier id = new Identifier();
        try {
            id.setValue(urlCodec.decode(pid, "UTF-8"));
        } catch (DecoderException ex) {
            throw new ServiceFailure("20000", ex.getMessage());
        } catch (UnsupportedEncodingException ex) {
            throw new ServiceFailure("20001", ex.getMessage());
        }
        String checksumAlgorithm = null;
        String query = request.getQueryString();
        if ((query != null) && (query.length() > 0)) {
            Matcher matcher = checksumPattern.matcher(query);
            if (matcher.find()) {
                checksumAlgorithm = matcher.group(1);
            }
        }
        Subject subject = new Subject();
        subject.setValue("public");
        Session auth = new Session();
        auth.setSubject(subject);
        Checksum checksum = null;

        checksum = mnRead.getChecksum(auth, id, checksumAlgorithm);

        return new ModelAndView("xmlChecksumViewResolver", "org.dataone.service.types.v1.Checksum", checksum);
    }
}