summaryrefslogtreecommitdiffstats
path: root/base/kra/functional/src/com/netscape/cms/servlet/test/DRMErrorInterceptor.java
blob: 7572acef5e3e0fe9e5b1eb0a280d2da95d5ef30d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.netscape.cms.servlet.test;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;

import org.jboss.resteasy.client.ClientResponse;
import org.jboss.resteasy.client.core.ClientErrorInterceptor;

import com.netscape.cms.servlet.base.CMSException;

public class DRMErrorInterceptor implements ClientErrorInterceptor  {

    public void handle(ClientResponse<?> response) {

        // handle HTTP code 4xx and 5xx
        int code = response.getResponseStatus().getStatusCode();
        if (code < 400) return;

        MultivaluedMap<String, String> headers = response.getHeaders();
        String contentType = headers.getFirst("Content-Type");

        // handle XML content only
        if (!contentType.startsWith(MediaType.TEXT_XML)) return;

        CMSException exception;

        try {
            // Requires RESTEasy 2.3.2
            // https://issues.jboss.org/browse/RESTEASY-652
            CMSException.Data data = response.getEntity(CMSException.Data.class);

            Class<?> clazz = Class.forName(data.className);
            exception = (CMSException) clazz.getConstructor(CMSException.Data.class).newInstance(data);

        } catch (Exception e) {
            e.printStackTrace();
            return;
        }

        throw exception;
    }

}