Package org.apache.wink.common.internal.http

Examples of org.apache.wink.common.internal.http.Accept


        } catch (IllegalArgumentException ignore) {
        }
    }

    public void testAcceptHeader() {
        Accept ah1 = Accept.valueOf("*/*;q=0.1,image/png,image/gif;q=0.512,text/plain;q=0.1234");
        List<ValuedMediaType> vmtList = ah1.getValuedMediaTypes();
        assertEquals("size", 4, vmtList.size());
        assertEquals("1. q-value", 0.1, vmtList.get(0).getQ());
        assertEquals("1. mime", MediaType.valueOf("*/*;q=0.1"), vmtList.get(0).getMediaType());
        assertEquals("2. q-value", 1.0, vmtList.get(1).getQ());
        assertEquals("2. mime", MediaTypeUtils.IMAGE_PNG, vmtList.get(1).getMediaType());
        assertEquals("3. q-value", 0.512, vmtList.get(2).getQ());
        assertEquals("3. mime", MediaType.valueOf("image/gif;q=0.512"), vmtList.get(2)
            .getMediaType());
        assertEquals("4. q-value", 0.123, vmtList.get(3).getQ());
        assertEquals("4. mime", MediaType.valueOf("text/plain;q=0.123"), vmtList.get(3)
            .getMediaType());

        List<MediaType> mtList = new ArrayList<MediaType>();
        mtList.add(MediaType.valueOf("*/*;q=0.1"));
        mtList.add(MediaType.valueOf("image/png"));
        mtList.add(MediaType.valueOf("image/gif;q=0.512"));
        mtList.add(MediaType.valueOf("text/plain;q=0.1234"));
        Accept ah2 = new Accept(mtList);
        assertEquals("toString", ah1.toString(), ah2.toString());
    }
View Full Code Here


        Accept ah2 = new Accept(mtList);
        assertEquals("toString", ah1.toString(), ah2.toString());
    }

    public void testEvaluation() {
        Accept acceptHeaderA =
            Accept
                .valueOf("application/atom+xml;type=entry;q=0.6, " + "application/atom+xml;q=0.714, "
                    + "application/json, "
                    + "application/pdf;q=0.712;ignored=qExtension");

        assertFalse("text/plain not acceptable", acceptHeaderA
            .isAcceptable(MediaType.TEXT_PLAIN_TYPE));

        assertTrue("json acceptable", acceptHeaderA.isAcceptable(MediaType.APPLICATION_JSON_TYPE));

        assertTrue("atom;entry acceptable", acceptHeaderA
            .isAcceptable(MediaTypeUtils.ATOM_ENTRY_TYPE));

        assertTrue("application/pdf acceptable", acceptHeaderA
            .isAcceptable(MediaTypeUtils.PDF_TYPE));

        Accept acceptHeaderB = Accept.valueOf("text/*, */*;q=0.1, text/plain;q=0");
        Accept acceptHeaderReverse = Accept.valueOf("text/plain;q=0, */*;q=0.1, text/*");

        assertTrue("image/jpeg acceptable", acceptHeaderB
            .isAcceptable(MediaTypeUtils.IMAGE_JPEG_TYPE));
        assertTrue("image/jpeg acceptable", acceptHeaderReverse
            .isAcceptable(MediaTypeUtils.IMAGE_JPEG_TYPE));

        assertTrue("text/html acceptable", acceptHeaderB.isAcceptable(MediaType
            .valueOf("text/html")));
        assertTrue("text/html acceptable", acceptHeaderReverse.isAcceptable(MediaType
            .valueOf("text/html")));

        assertFalse("text/plain not acceptable", acceptHeaderB
            .isAcceptable(MediaType.TEXT_PLAIN_TYPE));
        assertFalse("text/plain not acceptable", acceptHeaderReverse
            .isAcceptable(MediaType.TEXT_PLAIN_TYPE));
    }
View Full Code Here

        return acceptableLanguages;
    }

    public List<MediaType> getAcceptableMediaTypes() {
        if (acceptableMediaTypes == null) {
            Accept acceptHeader = getAcceptHeader();
            acceptableMediaTypes = acceptHeader.getSortedMediaTypes();
        }
        logger.debug("getAcceptableMediaTypes() returns {}", acceptableMediaTypes); //$NON-NLS-1$
        return acceptableMediaTypes;
    }
View Full Code Here

                acceptValue = requestHeader.get(0);
            }
        }
        try {
            logger.debug("Accept header is: {}", acceptValue); //$NON-NLS-1$
            Accept acceptHeader = Accept.valueOf(acceptValue);
            logger.debug("getAcceptHeader() returns {}", acceptHeader); //$NON-NLS-1$
            return acceptHeader;
        } catch (IllegalArgumentException e) {
            logger.debug("Illegal Accept request header: {}", e); //$NON-NLS-1$
            throw new WebApplicationException(e, 400);
View Full Code Here

public class AcceptHeaderDelegate implements HeaderDelegate<Accept> {

    private static final SoftConcurrentMap<String, Accept> cache = new SoftConcurrentMap<String, Accept>();
   
    public Accept fromString(String value) throws IllegalArgumentException {
        Accept cached = cache.get(value);
        if (cached != null) {
            return cached;
        }
       
        // if there is no Accept header it means that all media types are
        // acceptable
        if (value == null) {
            value = MediaType.WILDCARD;
        }
        List<MediaType> list = new LinkedList<MediaType>();
        String[] mediaTypes = StringUtils.fastSplit(value, ","); //$NON-NLS-1$
        for (String mediaRange : mediaTypes) {
            mediaRange = mediaRange.trim();
            if (mediaRange.length() == 0) {
                continue;
            }
            list.add(MediaType.valueOf(mediaRange));
        }
        return cache.put(value, new Accept(list));
    }
View Full Code Here

        return acceptableLanguages;
    }

    public List<MediaType> getAcceptableMediaTypes() {
        if (acceptableMediaTypes == null) {
            Accept acceptHeader = getAcceptHeader();
            acceptableMediaTypes = acceptHeader.getSortedMediaTypes();
        }
        logger.trace("getAcceptableMediaTypes() returns {}", acceptableMediaTypes); //$NON-NLS-1$
        return acceptableMediaTypes;
    }
View Full Code Here

                acceptValue = requestHeader.get(0);
            }
        }
        try {
            logger.trace("Accept header is: {}", acceptValue); //$NON-NLS-1$
            Accept acceptHeader = Accept.valueOf(acceptValue);
            logger.trace("getAcceptHeader() returns {}", acceptHeader); //$NON-NLS-1$
            return acceptHeader;
        } catch (IllegalArgumentException e) {
            logger.error(Messages.getMessage("illegalAcceptHeader", acceptValue), e);
            throw new WebApplicationException(e, Status.BAD_REQUEST);
View Full Code Here

        public Response get(@PathParam("variable") String typeName, @Context HttpHeaders headers) {
            if (typeName.equals("unknown") || typeName.equals("atom")) {
                return RuntimeDelegate.getInstance().createResponseBuilder()
                    .status(Response.Status.OK).entity("unknown string").build();
            } else if (typeName.equals("pdf")) {
                Accept accept = new Accept(headers.getAcceptableMediaTypes());
                if (!accept.isAcceptable(MediaTypeUtils.PDF_TYPE)) {
                    return RuntimeDelegate.getInstance().createResponseBuilder()
                        .status(Response.Status.NOT_ACCEPTABLE).build();
                }
                return RuntimeDelegate.getInstance().createResponseBuilder()
                    .status(Response.Status.OK).entity("pdf string")
View Full Code Here

TOP

Related Classes of org.apache.wink.common.internal.http.Accept

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.