Skip to content

Commit

Permalink
Adjust logging configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
acoburn committed Oct 13, 2023
1 parent 9eecd83 commit 628df25
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,25 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.trellisldp.webdav;

import static java.util.Optional.of;
package org.trellisldp.http;

import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.ExceptionMapper;
import jakarta.ws.rs.ext.Provider;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Provider
public class DebugExceptionMapper implements ExceptionMapper<Exception> {
public class WebApplicationExceptionMapper implements ExceptionMapper<WebApplicationException> {

@Override
public Response toResponse(final Exception err) {
err.printStackTrace();
private static final Logger LOGGER = LoggerFactory.getLogger(WebApplicationExceptionMapper.class);

if (err instanceof WebApplicationException) {
return ((WebApplicationException) err).getResponse();
}
return of(err).map(Throwable::getCause).filter(WebApplicationException.class::isInstance)
.map(WebApplicationException.class::cast).orElseGet(() -> new WebApplicationException(err)).getResponse();
@Override
public Response toResponse(final WebApplicationException err) {
LOGGER.debug("Unhandled web application exception", err);

return err.getResponse();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import static org.trellisldp.http.impl.HttpUtils.unskolemizeTriples;

import jakarta.ws.rs.ClientErrorException;
import jakarta.ws.rs.InternalServerErrorException;
import jakarta.ws.rs.NotAcceptableException;
import jakarta.ws.rs.NotFoundException;
import jakarta.ws.rs.RedirectionException;
Expand Down Expand Up @@ -383,8 +384,12 @@ private CompletionStage<ResponseBuilder> getLdpNr(final ResponseBuilder builder)
}

private static void copy(final InputStream from, final OutputStream to) throws IOException {
from.transferTo(to);
from.close();
if (from != null) {
from.transferTo(to);
from.close();
} else {
throw new InternalServerErrorException("Invalid data stream");
}
}

private CompletionStage<InputStream> getBinaryStream(final IRI dsid, final TrellisRequest req) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ public ResponseBuilder initialize(final Resource parent, final Resource child) {
throw new BadRequestException("Unsupported interaction model provided", status(BAD_REQUEST)
.link(UnsupportedInteractionModel.getIRIString(), LDP.constrainedBy.getIRIString()).build());
} else if (ldpType.equals(LDP.NonRDFSource) && rdfSyntax != null) {
LOGGER.error("Cannot save {} as a NonRDFSource with RDF syntax", getIdentifier());
throw new BadRequestException("Cannot save resource as a NonRDFSource with RDF syntax");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ public CompletionStage<ResponseBuilder> setResource(final ResponseBuilder builde

// It is not possible to change the LDP type to a type that is not a subclass
if (hasInteractionModelChangeRestriction(ldpType)) {
LOGGER.error("Cannot change the LDP type to {} for {}", ldpType, getIdentifier());
throw new ClientErrorException("Cannot change the LDP type to " + ldpType, status(CONFLICT).build());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ protected Application configure() {
System.setProperty(CONFIG_HTTP_BASE_URL, URL);

final ResourceConfig config = new ResourceConfig();
config.register(new WebApplicationExceptionMapper());
config.register(new TrellisHttpResource());
config.register(new TestAuthenticationFilter("testUser", "group"));
config.register(new CacheControlFilter());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ protected Application configure() {
System.setProperty(CONFIG_HTTP_BASE_URL, baseUri);

final ResourceConfig config = new ResourceConfig();
config.register(new WebApplicationExceptionMapper());
config.register(new TrellisHttpResource());
config.register(new CacheControlFilter());
config.register(new WebSubHeaderFilter());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ protected Application configure() {
System.setProperty(CONFIG_HTTP_BASE_URL, getBaseUrl());

final ResourceConfig config = new ResourceConfig();
config.register(new WebApplicationExceptionMapper());
config.register(new TrellisHttpResource());
config.register(new CacheControlFilter());
config.register(new TrellisHttpFilter());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ protected Application configure() {
System.clearProperty(CONFIG_HTTP_BASE_URL);

final ResourceConfig config = new ResourceConfig();
config.register(new WebApplicationExceptionMapper());
config.register(new TrellisHttpResource());
config.register(new TestAuthenticationFilter("testUser", "group"));
config.register(new CacheControlFilter());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,8 @@ private Resource checkResource(final Resource res) {
private Response handleException(final Throwable err) {
final Throwable cause = err.getCause();
if (cause instanceof WebApplicationException) return ((WebApplicationException) cause).getResponse();
LOGGER.error("Trellis WebDAV Error: {}", err.getMessage());
LOGGER.debug("WebDAV error", err);
LOGGER.debug("WebDAV error: {}", err.getMessage());
LOGGER.trace("WebDAV error", err);
return new WebApplicationException(err).getResponse();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.junit.jupiter.api.TestInstance;
import org.trellisldp.common.ServiceBundler;
import org.trellisldp.http.TrellisHttpResource;
import org.trellisldp.http.WebApplicationExceptionMapper;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class WebDAVNoBaseUrlTest extends AbstractWebDAVTest {
Expand Down Expand Up @@ -85,7 +86,7 @@ public Application configure() {
dav.init();

config.register(dav);
config.register(new DebugExceptionMapper());
config.register(new WebApplicationExceptionMapper());
config.register(new TestAuthnFilter("testUser", ""));
config.register(new TrellisWebDAVRequestFilter());
config.register(new TrellisWebDAVResponseFilter());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.junit.jupiter.api.TestInstance;
import org.trellisldp.common.ServiceBundler;
import org.trellisldp.http.TrellisHttpResource;
import org.trellisldp.http.WebApplicationExceptionMapper;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class WebDAVTest extends AbstractWebDAVTest {
Expand All @@ -42,7 +43,7 @@ protected Application configure() {
dav.init();

config.register(dav);
config.register(new DebugExceptionMapper());
config.register(new WebApplicationExceptionMapper());
config.register(new TrellisWebDAVRequestFilter());
config.register(new TrellisWebDAVResponseFilter());
config.register(new TrellisHttpResource());
Expand Down

0 comments on commit 628df25

Please sign in to comment.