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.servlet.ServletContext; 023import javax.ws.rs.core.Link; 024import javax.ws.rs.core.UriInfo; 025import javax.ws.rs.ext.ExceptionMapper; 026 027import org.fcrepo.kernel.api.exception.ConstraintViolationException; 028 029/** 030 * Abstract class for constraint violation subclasses 031 * 032 * @author whikloj 033 * @since 2015-06-24 034 * @param <T> Throwable subclass of ConstraintViolationException 035 */ 036public abstract class ConstraintExceptionMapper<T extends ConstraintViolationException> implements ExceptionMapper<T> { 037 038 /** 039 * Where the RDF exception files sit. 040 */ 041 private static final String CONSTRAINT_DIR = "/static/constraints/"; 042 043 /** 044 * Creates a constrainedBy link header with the appropriate RDF URL for the exception. 045 * 046 * @param e ConstraintViolationException Exception which implements the buildContraintUri method. 047 * @param context ServletContext ServletContext that we're running in. 048 * @param uriInfo UriInfo UriInfo from the ExceptionMapper. 049 * @return Link A http://www.w3.org/ns/ldp#constrainedBy link header 050 */ 051 public static Link buildConstraintLink(final ConstraintViolationException e, 052 final ServletContext context, 053 final UriInfo uriInfo) { 054 055 String path = context.getContextPath(); 056 if (path.equals("/")) { 057 path = ""; 058 } 059 060 final String constraintURI = uriInfo == null ? "" : String.format("%s://%s%s%s%s.rdf", 061 uriInfo.getBaseUri().getScheme(), uriInfo.getBaseUri().getAuthority(), path, 062 CONSTRAINT_DIR, e.getClass().toString().substring(e.getClass().toString().lastIndexOf('.') + 1)); 063 return Link.fromUri(constraintURI).rel(CONSTRAINED_BY.getURI()).build(); 064 } 065 066}