Search in sources :

Example 86 with UniformInterfaceException

use of com.sun.jersey.api.client.UniformInterfaceException in project ANNIS by korpling.

the class FlatQueryBuilder method getAvailableMetaNames.

public Set<String> getAvailableMetaNames() {
    Set<String> result = new TreeSet<>();
    WebResource service = Helper.getAnnisWebResource();
    // get current corpus selection
    Set<String> corpusSelection = cp.getState().getSelectedCorpora().getValue();
    if (service != null) {
        try {
            List<AnnisAttribute> atts = new LinkedList<>();
            for (String corpus : corpusSelection) {
                atts.addAll(service.path("query").path("corpora").path(corpus).path("annotations").get(new GenericType<List<AnnisAttribute>>() {
                }));
            }
            for (AnnisAttribute a : atts) {
                if (a.getType() == AnnisAttribute.Type.meta) {
                    String aa = killNamespace(a.getName());
                    result.add(aa);
                }
            }
        } catch (ClientHandlerException ex) {
            log.error(null, ex);
        } catch (UniformInterfaceException ex) {
            log.error(null, ex);
        }
    }
    return result;
}
Also used : ClientHandlerException(com.sun.jersey.api.client.ClientHandlerException) GenericType(com.sun.jersey.api.client.GenericType) UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) TreeSet(java.util.TreeSet) AnnisAttribute(annis.service.objects.AnnisAttribute) WebResource(com.sun.jersey.api.client.WebResource) LinkedList(java.util.LinkedList)

Example 87 with UniformInterfaceException

use of com.sun.jersey.api.client.UniformInterfaceException in project ANNIS by korpling.

the class SearchOptionsPanel method getSegmentationNamesFromService.

private static List<String> getSegmentationNamesFromService(Set<String> corpora) {
    List<String> segNames = new ArrayList<>();
    WebResource service = Helper.getAnnisWebResource();
    if (service != null) {
        for (String corpus : corpora) {
            try {
                SegmentationList segList = service.path("query").path("corpora").path(Helper.encodeJersey(corpus)).path("segmentation-names").get(SegmentationList.class);
                segNames.addAll(segList.getSegmentatioNames());
            } catch (UniformInterfaceException ex) {
                if (ex.getResponse().getStatus() == 403) {
                    log.debug("Did not have access rights to query segmentation names for corpus", ex);
                } else {
                    log.warn("Could not query segmentation names for corpus", ex);
                }
            }
        }
    }
    return segNames;
}
Also used : UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) SegmentationList(annis.service.objects.SegmentationList) ArrayList(java.util.ArrayList) WebResource(com.sun.jersey.api.client.WebResource)

Example 88 with UniformInterfaceException

use of com.sun.jersey.api.client.UniformInterfaceException in project ANNIS by korpling.

the class BinaryRequestHandler method sendResponse.

public void sendResponse(VaadinSession session, VaadinRequest request, VaadinResponse pureResponse, boolean sendContent) throws IOException {
    if (!(pureResponse instanceof VaadinServletResponse)) {
        pureResponse.sendError(500, "Binary requests only work with servlets");
    }
    VaadinServletResponse response = (VaadinServletResponse) pureResponse;
    Map<String, String[]> binaryParameter = request.getParameterMap();
    String toplevelCorpusName = binaryParameter.get("toplevelCorpusName")[0];
    String documentName = binaryParameter.get("documentName")[0];
    String mimeType = null;
    if (binaryParameter.containsKey("mime")) {
        mimeType = binaryParameter.get("mime")[0];
    }
    try {
        // always set the buffer size to the same one we will use for coyping,
        // otherwise we won't notice any client disconnection
        response.reset();
        response.setCacheTime(-1);
        response.resetBuffer();
        // 4K
        response.setBufferSize(BUFFER_SIZE);
        String requestedRangeRaw = request.getHeader("Range");
        WebResource binaryRes = Helper.getAnnisWebResource().path("query").path("corpora").path(urlPathEscape.escape(toplevelCorpusName)).path(urlPathEscape.escape(documentName)).path("binary");
        WebResource metaBinaryRes = Helper.getAnnisWebResource().path("meta").path("binary").path(urlPathEscape.escape(toplevelCorpusName)).path(urlPathEscape.escape(documentName));
        // tell client that we support byte ranges
        response.setHeader("Accept-Ranges", "bytes");
        Preconditions.checkNotNull(mimeType, "No mime type given (parameter \"mime\"");
        AnnisBinaryMetaData meta = getMatchingMetadataFromService(metaBinaryRes, mimeType);
        if (meta == null) {
            response.sendError(404, "Binary file not found");
            return;
        }
        ContentRange fullRange = new ContentRange(0, meta.getLength() - 1, meta.getLength());
        ContentRange r = fullRange;
        try {
            if (requestedRangeRaw != null) {
                List<ContentRange> requestedRanges = ContentRange.parseFromHeader(requestedRangeRaw, meta.getLength(), 1);
                if (!requestedRanges.isEmpty()) {
                    r = requestedRanges.get(0);
                }
            }
            long contentLength = (r.getEnd() - r.getStart() + 1);
            boolean useContentRange = !fullRange.equals(r);
            response.setContentType(meta.getMimeType());
            if (useContentRange) {
                response.setHeader("Content-Range", r.toString());
            }
            response.setContentLength((int) contentLength);
            response.setStatus(useContentRange ? 206 : 200);
            response.flushBuffer();
            if (sendContent) {
                try (OutputStream out = response.getOutputStream()) {
                    writeFromServiceToClient(r.getStart(), contentLength, binaryRes, out, mimeType);
                }
            }
        } catch (ContentRange.InvalidRangeException ex) {
            response.setHeader("Content-Range", "bytes */" + meta.getLength());
            response.sendError(416, "Requested range not satisfiable: " + ex.getMessage());
            return;
        }
    } catch (IOException ex) {
        log.warn("IOException in BinaryRequestHandler", ex);
        response.setStatus(500);
    } catch (ClientHandlerException | UniformInterfaceException ex) {
        log.error(null, ex);
        response.setStatus(500);
    }
}
Also used : ClientHandlerException(com.sun.jersey.api.client.ClientHandlerException) OutputStream(java.io.OutputStream) WebResource(com.sun.jersey.api.client.WebResource) IOException(java.io.IOException) VaadinServletResponse(com.vaadin.server.VaadinServletResponse) UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) AnnisBinaryMetaData(annis.service.objects.AnnisBinaryMetaData)

Example 89 with UniformInterfaceException

use of com.sun.jersey.api.client.UniformInterfaceException in project ANNIS by korpling.

the class LoginServletRequestHandler method doPost.

private void doPost(VaadinSession session, VaadinRequest request, VaadinResponse response) throws IOException {
    response.setContentType("text/html");
    String username = request.getParameter("annis-login-user");
    String password = request.getParameter("annis-login-password");
    if (username != null && password != null) {
        // forget any old user information
        session.getSession().removeAttribute(AnnisBaseUI.USER_KEY);
        session.getSession().removeAttribute(AnnisBaseUI.USER_LOGIN_ERROR);
        // get the URL for the REST service
        Object annisServiceURLObject = session.getSession().getAttribute(AnnisBaseUI.WEBSERVICEURL_KEY);
        if (annisServiceURLObject == null || !(annisServiceURLObject instanceof String)) {
            log.warn("AnnisWebService.URL was not set as init parameter in web.xml");
        }
        String webserviceURL = (String) annisServiceURLObject;
        try {
            AnnisUser user = new AnnisUser(username, password);
            WebResource res = user.getClient().resource(webserviceURL).path("admin").path("is-authenticated");
            if ("true".equalsIgnoreCase(res.get(String.class))) {
                // everything ok, save this user configuration for re-use
                Helper.setUser(user);
            }
        } catch (ClientHandlerException ex) {
            session.getSession().setAttribute(AnnisBaseUI.USER_LOGIN_ERROR, "Authentification error: " + ex.getMessage());
            // bad gateway
            response.setStatus(502);
        } catch (LoginDataLostException ex) {
            session.getSession().setAttribute(AnnisBaseUI.USER_LOGIN_ERROR, "Lost password in memory. Sorry.");
            // server error
            response.setStatus(500);
        } catch (UniformInterfaceException ex) {
            if (ex.getResponse().getStatus() == Response.Status.UNAUTHORIZED.getStatusCode()) {
                session.getSession().setAttribute(AnnisBaseUI.USER_LOGIN_ERROR, "Username or password wrong");
                response.setStatus(Response.Status.UNAUTHORIZED.getStatusCode());
            } else if (ex.getResponse().getStatus() == Response.Status.FORBIDDEN.getStatusCode()) {
                session.getSession().setAttribute(AnnisBaseUI.USER_LOGIN_ERROR, "Account has expired");
                // Forbidden
                response.setStatus(Response.Status.FORBIDDEN.getStatusCode());
            } else {
                log.error(null, ex);
                session.getSession().setAttribute(AnnisBaseUI.USER_LOGIN_ERROR, "Unexpected exception: " + ex.getMessage());
                response.setStatus(500);
            }
        }
        try (OutputStreamWriter writer = new OutputStreamWriter(response.getOutputStream(), Charsets.UTF_8)) {
            String html = Resources.toString(LoginServletRequestHandler.class.getResource("closelogin.html"), Charsets.UTF_8);
            CharStreams.copy(new StringReader(html), writer);
        }
    }
// end if login attempt
}
Also used : ClientHandlerException(com.sun.jersey.api.client.ClientHandlerException) UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) LoginDataLostException(annis.libgui.LoginDataLostException) StringReader(java.io.StringReader) WebResource(com.sun.jersey.api.client.WebResource) OutputStreamWriter(java.io.OutputStreamWriter) AnnisUser(annis.libgui.AnnisUser)

Example 90 with UniformInterfaceException

use of com.sun.jersey.api.client.UniformInterfaceException in project ANNIS by korpling.

the class CSVExporter method convertText.

@Override
public Exception convertText(String queryAnnisQL, int contextLeft, int contextRight, Set<String> corpora, List<String> keys, String argsAsString, boolean alignmc, WebResource annisResource, Writer out, EventBus eventBus, Map<String, CorpusConfig> corpusConfigs) {
    try {
        WebResource res = annisResource.path("search").path("matrix").queryParam("csv", "true").queryParam("corpora", StringUtils.join(corpora, ",")).queryParam("q", Helper.encodeJersey(queryAnnisQL));
        if (argsAsString.startsWith("metakeys=")) {
            res = res.queryParam("metakeys", argsAsString.substring("metakeys".length() + 1));
        }
        try (InputStream result = res.get(InputStream.class)) {
            IOUtils.copy(result, out);
        }
        out.flush();
        return null;
    } catch (UniformInterfaceException | ClientHandlerException | IOException ex) {
        return ex;
    }
}
Also used : ClientHandlerException(com.sun.jersey.api.client.ClientHandlerException) UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) InputStream(java.io.InputStream) WebResource(com.sun.jersey.api.client.WebResource) IOException(java.io.IOException)

Aggregations

UniformInterfaceException (com.sun.jersey.api.client.UniformInterfaceException)172 WebResource (com.sun.jersey.api.client.WebResource)128 ClientResponse (com.sun.jersey.api.client.ClientResponse)74 Test (org.junit.Test)65 JSONObject (org.codehaus.jettison.json.JSONObject)45 ClientHandlerException (com.sun.jersey.api.client.ClientHandlerException)34 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)20 SOSFailure (com.emc.storageos.vasa.fault.SOSFailure)18 ArrayList (java.util.ArrayList)17 LinkedList (java.util.LinkedList)16 JobId (org.apache.hadoop.mapreduce.v2.api.records.JobId)14 Job (org.apache.hadoop.mapreduce.v2.app.job.Job)14 IOException (java.io.IOException)12 Client (com.sun.jersey.api.client.Client)10 List (java.util.List)10 AnnisAttribute (annis.service.objects.AnnisAttribute)9 GenericType (com.sun.jersey.api.client.GenericType)8 Application (org.apache.hadoop.yarn.server.nodemanager.containermanager.application.Application)7 MockNM (org.apache.hadoop.yarn.server.resourcemanager.MockNM)7 JerseyTest (com.sun.jersey.test.framework.JerseyTest)6