Package org.apache.marmotta.commons.http

Examples of org.apache.marmotta.commons.http.ContentType


        Assert.assertEquals(offered1, MarmottaHttpUtils.bestContentType(offered,accepted));
    }

    @Test
    public void testGetBestMatch3() throws Exception {
        ContentType offered1 = new ContentType("text","html");
        ContentType offered2 = new ContentType("application","rdf+json");
        ContentType offered3 = new ContentType("application","ld+json");

        List<ContentType> offered = new ArrayList<ContentType>(3);
        offered.add(offered1);
        offered.add(offered2);
        offered.add(offered3);

        ContentType accepted1 = new ContentType("application","ld+json");
        ContentType accepted2 = new ContentType("*","*");
        List<ContentType> accepted = new ArrayList<ContentType>(2);
        accepted.add(accepted1);
        accepted.add(accepted2);

        Assert.assertEquals(offered3, MarmottaHttpUtils.bestContentType(offered,accepted));
View Full Code Here


    /**
     * return the content type that will be produced
     * @return
     */
    public ContentType getContentType() {
        return new ContentType("application","link-format");
    }
View Full Code Here

            try {
                final Date date = MementoUtils.MEMENTO_DATE_FORMAT.parse(date_string);

                final URI resource = ResourceUtils.getUriResource(conn, resource_string);

                final ContentType type = getContentType(types_string);

                //get serializer
                final RDFFormat serializer = lmfIOService.getSerializer(type.getMime());

                //create response serialisation
                StreamingOutput entity = new StreamingOutput() {
                    @Override
                    public void write(OutputStream output) throws IOException, WebApplicationException {
                        RDFWriter writer = Rio.createWriter(serializer, output);
                        try {
                            RepositoryConnection con = versioningService.getSnapshot(date);
                            URI subject = con.getValueFactory().createURI(resource.stringValue());
                            try {
                                con.exportStatements(subject,null,null,true,writer);
                            } catch (RepositoryException e) {
                                throw new WebApplicationException(e, Response.Status.INTERNAL_SERVER_ERROR);
                            } catch (RDFHandlerException e) {
                                throw new IOException("error while writing RDF data to stream");
                            } finally {
                                con.commit();
                                con.close();
                            }
                        } catch (RepositoryException e) {
                            throw new WebApplicationException(e, Response.Status.INTERNAL_SERVER_ERROR);
                        }
                    }
                };

                //get versions
                MementoVersionSet versions = mementoService.getVersionSet(resource, date);

                //build version links
                Set<String> links = versions.buildLinks(configurationService.getBaseUri());

                //add timegate link
                links.add("<" + MementoUtils.timegateURI(resource_string, configurationService.getBaseUri()) + ">;rel=timegate");

                //add timemap link
                links.add("<" + MementoUtils.timemapURI(resource_string, configurationService.getBaseUri()) + ">;rel=timemap");

                //create response
                return Response
                        .ok()
                        .header("Link", CollectionUtils.fold(links," ,"))
                        .header("Content-Type", type.toString())
                        .header("Memento-Datetime", versions.getCurrent().getCommitTime().toString())
                        .entity(entity)
                        .build();

            } catch (ParseException e) {
View Full Code Here

     * @throws IllegalArgumentException if no type is supported
     */
    private ContentType getContentType(String types) throws IllegalArgumentException {
        List<ContentType> acceptedTypes = LMFHttpUtils.parseAcceptHeader(types);
        List<ContentType> offeredTypes  = LMFHttpUtils.parseStringList(lmfIOService.getProducedTypes());
        ContentType type = LMFHttpUtils.bestContentType(offeredTypes,acceptedTypes);
        if(type == null) throw new IllegalArgumentException("Requested type is not supported");
        return type;
    }
View Full Code Here

    public Response selectGet(@QueryParam("query") String query, @QueryParam("output") String resultType, @Context HttpServletRequest request) {
        if(resultType == null) {
            List<ContentType> acceptedTypes = LMFHttpUtils.parseAcceptHeader(request.getHeader("Accept"));
            List<ContentType> offeredTypes  = LMFHttpUtils.parseStringList(Lists.newArrayList("application/sparql-results+xml","application/sparql-results+json","text/html", "application/rdf+xml", "text/csv"));

            ContentType bestType = LMFHttpUtils.bestContentType(offeredTypes,acceptedTypes);

            if(bestType != null) {
                resultType = bestType.getMime();
            }
        }

        try {
            if(resultType != null) {
View Full Code Here

        try {
            if(resultType == null) {
                List<ContentType> acceptedTypes = LMFHttpUtils.parseAcceptHeader(request.getHeader("Accept"));
                List<ContentType> offeredTypes  = LMFHttpUtils.parseStringList(Lists.newArrayList("application/sparql-results+xml","application/sparql-results+json","text/html", "application/rdf+xml", "text/csv"));

                ContentType bestType = LMFHttpUtils.bestContentType(offeredTypes,acceptedTypes);

                if(bestType != null) {
                    resultType = bestType.getMime();
                }
            }
            if(resultType != null) {
                if (StringUtils.isNotBlank(query))
                    return buildQueryResponse(resultType, query);
View Full Code Here

        try {
            if(resultType == null) {
                List<ContentType> acceptedTypes = LMFHttpUtils.parseAcceptHeader(request.getHeader("Accept"));
                List<ContentType> offeredTypes  = LMFHttpUtils.parseStringList(Lists.newArrayList("application/sparql-results+xml","application/sparql-results+json","text/html", "application/rdf+xml", "text/csv"));

                ContentType bestType = LMFHttpUtils.bestContentType(offeredTypes,acceptedTypes);

                if(bestType != null) {
                    resultType = bestType.getMime();
                }
            }

            if(resultType != null) {
                String query = CharStreams.toString(request.getReader());
View Full Code Here

                return Response.ok().build();
            } else {
                if (resultType == null) {
                    List<ContentType> acceptedTypes = LMFHttpUtils.parseAcceptHeader(request.getHeader("Accept"));
                    List<ContentType> offeredTypes = LMFHttpUtils.parseStringList(Lists.newArrayList("*/*", "text/html"));
                    ContentType bestType = LMFHttpUtils.bestContentType(offeredTypes, acceptedTypes);
                    if (bestType != null) {
                        resultType = bestType.getMime();
                    }
                }
                if (parseSubType(resultType).equals("html"))
                    return Response.seeOther(new URI(configurationService.getServerUri() + "sparql/admin/update.html")).build();
                else
View Full Code Here

        } else {
            acceptedTypes = LMFHttpUtils.parseAcceptHeader(types);
        }
        List<ContentType> offeredTypes  = LMFHttpUtils.parseStringList(exportService.getProducedTypes());

        final ContentType bestType = LMFHttpUtils.bestContentType(offeredTypes,acceptedTypes);

        // create a file name for the export, preferrably with a good extension ...
        String fileName;
        if(context_string != null) {
            String[] components = context_string.split("/");
            fileName = components[components.length-1] + "-export-" + DateUtils.FILENAME_FORMAT.format(new Date());
        } else {
            fileName = "lmf-export-" + DateUtils.FILENAME_FORMAT.format(new Date());
        }

        if(bestType != null) {
            RDFFormat format = RDFFormat.forMIMEType(bestType.getMime());
            if(format != null) {
                fileName += "." + format.getDefaultFileExtension();
            }

            URI context = null;
            if(context_string != null) {
                try {
                    RepositoryConnection conn = sesameService.getConnection();
                    try {
                        conn.begin();
                        context = ResourceUtils.getUriResource(conn,context_string);
                    } finally {
                        conn.commit();
                        conn.close();
                    }
                } catch (RepositoryException e) {
                    handleRepositoryException(e,ExportWebService.class);
                }

                if(context == null) return Response.status(Response.Status.NOT_FOUND).entity("the context given as argument could not be found").build();

            } else {
                context = null;
            }
            final URI fcontext = context;

            StreamingOutput entity = new StreamingOutput() {
                @Override
                public void write(OutputStream output) throws IOException, WebApplicationException {
                    try {
                        //FIXME: html should not be exported, but rendered?
                        exportService.exportData(output,fcontext,bestType.getMime());
                    } catch (UnsupportedExporterException e) {
                        throw new WebApplicationException(e, Response.Status.NOT_ACCEPTABLE);
                    }
                }
            };

            return Response
                    .status(Response.Status.OK)
                    .header("Content-Type", bestType.getMime())
                    .header("Content-Disposition", "attachment; filename=\""+fileName+"\"")
                    .entity(entity)
                    .build();

        } else
View Full Code Here

            acceptedTypes = LMFHttpUtils.parseAcceptHeader(format);
        } else {
            acceptedTypes = LMFHttpUtils.parseAcceptHeader(accept);
        }
        List<ContentType> offeredTypes  = LMFHttpUtils.parseStringList(exportService.getProducedTypes());
        offeredTypes.removeAll(Collections.unmodifiableList(Arrays.asList(new ContentType("text", "html"), new ContentType("application", "xhtml+xml"))));
        final ContentType bestType = LMFHttpUtils.bestContentType(offeredTypes, acceptedTypes);
        return new URI(configurationService.getBaseUri() + "export/download?context=" + uri + "&format=" + bestType.getMime());
    }
View Full Code Here

TOP

Related Classes of org.apache.marmotta.commons.http.ContentType

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.