Java tutorial
/* * 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.IOException; import java.util.Set; import javax.servlet.http.HttpServletResponse; import javax.xml.parsers.ParserConfigurationException; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; 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.NotImplemented; import org.dataone.service.exceptions.ServiceFailure; import org.dataone.service.exceptions.SynchronizationFailed; import org.dataone.service.exceptions.UnsupportedType; import org.dataone.service.mn.tier1.v1.MNRead; import org.dataone.service.types.v1.Session; import org.dataone.service.util.ExceptionHandler; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Controller; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; import org.xml.sax.SAXException; /** * * @author waltz */ @Controller public class ErrorController { @Autowired @Qualifier("mnReadService") MNRead mnRead; @RequestMapping(value = { "/v1/error/", "/v1/error" }, method = RequestMethod.POST) public void fail(MultipartHttpServletRequest fileRequest, HttpServletResponse response) throws InvalidSystemMetadata, InvalidToken, ServiceFailure, NotAuthorized, IdentifierNotUnique, UnsupportedType, InsufficientResources, NotImplemented, InvalidRequest { Session session = new Session(); MultipartFile messageMultipart = null; SynchronizationFailed message; Set<String> keys = fileRequest.getFileMap().keySet(); for (String key : keys) { if (key.equalsIgnoreCase("message")) { messageMultipart = fileRequest.getFileMap().get(key); } } if (messageMultipart != null) { try { message = (SynchronizationFailed) ExceptionHandler.deserializeXml(messageMultipart.getInputStream(), "something broke"); } catch (IOException ex) { throw new InvalidSystemMetadata("15001", ex.getMessage()); } catch (SAXException ex) { throw new InvalidSystemMetadata("15001", ex.getMessage()); } catch (ParserConfigurationException ex) { throw new InvalidSystemMetadata("15001", ex.getMessage()); } } else { throw new InvalidSystemMetadata("15005", "System Metadata was not found as Part of Multipart Mime message"); } mnRead.synchronizationFailed(session, message); } }