001/* 002 * Licensed to DuraSpace under one or more contributor license agreements. 003 * See the NOTICE file distributed with this work for additional information 004 * regarding copyright ownership. 005 * 006 * DuraSpace licenses this file to you under the Apache License, 007 * Version 2.0 (the "License"); you may not use this file except in 008 * compliance with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.fcrepo.http.commons.exceptionhandlers; 019 020import static org.fcrepo.kernel.api.RdfLexicon.CONSTRAINED_BY; 021 022import javax.ws.rs.core.Link; 023import javax.ws.rs.core.UriInfo; 024import javax.ws.rs.ext.ExceptionMapper; 025 026import org.fcrepo.kernel.api.exception.ConstraintViolationException; 027 028/** 029 * Abstract class for constraint violation subclasses 030 * 031 * @author whikloj 032 * @since 2015-06-24 033 * @param <T> Throwable subclass of ConstraintViolationException 034 */ 035public abstract class ConstraintExceptionMapper<T extends ConstraintViolationException> implements ExceptionMapper<T> { 036 037 /** 038 * Where the RDF exception files sit. 039 */ 040 private static final String CONSTRAINT_DIR = "/static/constraints/"; 041 042 /** 043 * Creates a constrainedBy link header with the appropriate RDF URL for the exception. 044 * 045 * @param e ConstraintViolationException Exception which implements the buildContraintUri method. 046 * @param uriInfo UriInfo UriInfo from the ExceptionMapper. 047 * @return Link A http://www.w3.org/ns/ldp#constrainedBy link header 048 */ 049 public static Link buildConstraintLink(final ConstraintViolationException e, final UriInfo uriInfo) { 050 final String constraintURI = uriInfo == null ? "" : String.format("%s://%s%s%s.rdf", 051 uriInfo.getBaseUri().getScheme(), uriInfo.getBaseUri().getAuthority(), 052 CONSTRAINT_DIR, e.getClass().toString().substring(e.getClass().toString().lastIndexOf('.') + 1)); 053 return Link.fromUri(constraintURI).rel(CONSTRAINED_BY.getURI()).build(); 054 } 055 056}