Java Code Examples for javax.ws.rs.core.Variant#getLanguage()

The following examples show how to use javax.ws.rs.core.Variant#getLanguage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: VariantListBuilderImplTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
private boolean verifyVariant(List<Variant> vs, Variant var) {
    for (Variant v : vs) {

        if (v.getLanguage() == null
            && v.getEncoding() == null
            && v.getMediaType() == null) {
            return false;
        }
        boolean encodCheck = v.getEncoding() == null && var.getEncoding() == null
                             || v.getEncoding().equals(var.getEncoding());
        boolean langCheck = v.getLanguage() == null && var.getLanguage() == null
                            || v.getLanguage().equals(var.getLanguage());
        boolean typeCheck = v.getMediaType() == null && var.getMediaType() == null
                            || v.getMediaType().equals(var.getMediaType());
        if (encodCheck && langCheck && typeCheck) {
            return true;
        }
    }

    return false;
}
 
Example 2
Source File: VariantHelper.java    From ldp4j with Apache License 2.0 5 votes vote down vote up
private static boolean hasMatchingLanguage(Variant supported, Variant required) {
	Locale requiredLanguage = required.getLanguage();
	Locale supportedLanguage = supported.getLanguage();
	return
		requiredLanguage == null ||
		supportedLanguage ==null ||
		isLanguageMatched(supportedLanguage, requiredLanguage);
}
 
Example 3
Source File: VariantUtils.java    From ldp4j with Apache License 2.0 5 votes vote down vote up
private static String getLanguage(Variant v) {
	String language="*";
	Locale locale = v.getLanguage();
	if(locale!=null) {
		language=locale.toString();
	}
	return language;
}
 
Example 4
Source File: EndpointControllerUtils.java    From ldp4j with Apache License 2.0 5 votes vote down vote up
static void populateResponseBody(ResponseBuilder builder, String entity, Variant variant, boolean includeEntity) {
	MediaType mediaType = variant.getMediaType();

	String charsetName=mediaType.getParameters().get(MediaType.CHARSET_PARAMETER);
	Charset charset=StandardCharsets.UTF_8;
	if(charsetName!=null && !charsetName.isEmpty() && Charset.isSupported(charsetName)) {
		charset=Charset.forName(charsetName);
	} else {
		LOGGER.error("Missing of invalid charset information {}",mediaType);
		charsetName=charset.name();
	}

	MediaType target=
		Configuration.includeCharsetInformation()?
			mediaType.withCharset(charsetName):
			new MediaType(mediaType.getType(),mediaType.getSubtype());

	byte[] bytes = entity.getBytes(charset);
	builder.
		type(target).
		header(MoreHttp.CONTENT_LENGTH_HEADER,bytes.length);

	if(variant.getLanguage()!=null) {
		builder.language(variant.getLanguage());
	}

	if(includeEntity) {
		builder.entity(new ByteArrayInputStream(bytes));
	}
}
 
Example 5
Source File: RequestImpl.java    From cxf with Apache License 2.0 5 votes vote down vote up
public Variant selectVariant(List<Variant> vars) throws IllegalArgumentException {
    if (vars == null || vars.isEmpty()) {
        throw new IllegalArgumentException("List of Variants is either null or empty");
    }
    List<MediaType> acceptMediaTypes = headers.getAcceptableMediaTypes();
    List<Locale> acceptLangs = headers.getAcceptableLanguages();
    List<String> acceptEncs = parseAcceptEnc(
        headers.getRequestHeaders().getFirst(HttpHeaders.ACCEPT_ENCODING));
    List<Variant> requestVariants = sortAllCombinations(acceptMediaTypes, acceptLangs, acceptEncs);
    List<Object> varyValues = new LinkedList<>();
    for (Variant requestVar : requestVariants) {
        for (Variant var : vars) {
            MediaType mt = var.getMediaType();
            Locale lang = var.getLanguage();
            String enc = var.getEncoding();

            boolean mtMatched = mt == null || requestVar.getMediaType().isCompatible(mt);
            if (mtMatched) {
                handleVaryValues(varyValues, HttpHeaders.ACCEPT);
            }

            boolean langMatched = lang == null || isLanguageMatched(requestVar.getLanguage(), lang);
            if (langMatched) {
                handleVaryValues(varyValues, HttpHeaders.ACCEPT_LANGUAGE);
            }

            boolean encMatched = acceptEncs.isEmpty() || enc == null 
                || isEncMatached(requestVar.getEncoding(), enc);
            if (encMatched) {
                handleVaryValues(varyValues, HttpHeaders.ACCEPT_ENCODING);
            }

            if (mtMatched && encMatched && langMatched) {
                addVaryHeader(varyValues);
                return var;
            }
        }
    }
    return null;
}