use of javax.ws.rs.core.MediaType in project OpenAttestation by OpenAttestation.
the class ApiExceptionTest method testGetHttpReasonPhrase.
@Test
public void testGetHttpReasonPhrase() {
MediaType contentType = new MediaType();
byte[] content = new byte[] { 0x30, 0x31 };
ApiResponse response = new ApiResponse(404, "Not Found", contentType, content);
ApiException apiexception = new ApiException(response, "error!");
assertEquals("Not Found", apiexception.getHttpReasonPhrase());
}
use of javax.ws.rs.core.MediaType in project OpenAttestation by OpenAttestation.
the class ApacheHttpClient method readResponse.
private ApiResponse readResponse(HttpResponse response) throws IOException {
MediaType contentType = createMediaType(response);
byte[] content = null;
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream contentStream = entity.getContent();
if (contentStream != null) {
content = IOUtils.toByteArray(contentStream);
contentStream.close();
}
}
return new ApiResponse(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase(), contentType, content);
}
use of javax.ws.rs.core.MediaType in project jersey by jersey.
the class HttpAuthenticationFilter method repeatRequest.
/**
* Repeat the {@code request} with provided {@code newAuthorizationHeader}
* and update the {@code response} with newest response data.
*
* @param request Request context.
* @param response Response context (will be updated with the new response data).
* @param newAuthorizationHeader {@code Authorization} header that should be added to the new request.
* @return {@code true} is the authentication was successful ({@code true} if 401 response code was not returned;
* {@code false} otherwise).
*/
static boolean repeatRequest(ClientRequestContext request, ClientResponseContext response, String newAuthorizationHeader) {
Client client = request.getClient();
String method = request.getMethod();
MediaType mediaType = request.getMediaType();
URI lUri = request.getUri();
WebTarget resourceTarget = client.target(lUri);
Invocation.Builder builder = resourceTarget.request(mediaType);
MultivaluedMap<String, Object> newHeaders = new MultivaluedHashMap<String, Object>();
for (Map.Entry<String, List<Object>> entry : request.getHeaders().entrySet()) {
if (HttpHeaders.AUTHORIZATION.equals(entry.getKey())) {
continue;
}
newHeaders.put(entry.getKey(), entry.getValue());
}
newHeaders.add(HttpHeaders.AUTHORIZATION, newAuthorizationHeader);
builder.headers(newHeaders);
builder.property(REQUEST_PROPERTY_FILTER_REUSED, "true");
Invocation invocation;
if (request.getEntity() == null) {
invocation = builder.build(method);
} else {
invocation = builder.build(method, Entity.entity(request.getEntity(), request.getMediaType()));
}
Response nextResponse = invocation.invoke();
if (nextResponse.hasEntity()) {
response.setEntityStream(nextResponse.readEntity(InputStream.class));
}
MultivaluedMap<String, String> headers = response.getHeaders();
headers.clear();
headers.putAll(nextResponse.getStringHeaders());
response.setStatus(nextResponse.getStatus());
return response.getStatus() != Response.Status.UNAUTHORIZED.getStatusCode();
}
use of javax.ws.rs.core.MediaType in project jersey by jersey.
the class OutboundMessageContext method getAcceptableMediaTypes.
/**
* Get a list of media types that are acceptable for the message.
*
* @return a read-only list of requested message media types sorted according
* to their q-value, with highest preference first.
*/
@SuppressWarnings("unchecked")
public List<MediaType> getAcceptableMediaTypes() {
final List<Object> values = headers.get(HttpHeaders.ACCEPT);
if (values == null || values.isEmpty()) {
return WILDCARD_ACCEPTABLE_TYPE_SINGLETON_LIST;
}
final List<MediaType> result = new ArrayList<>(values.size());
final RuntimeDelegate rd = RuntimeDelegate.getInstance();
boolean conversionApplied = false;
for (final Object value : values) {
try {
if (value instanceof MediaType) {
final AcceptableMediaType _value = AcceptableMediaType.valueOf((MediaType) value);
// true if value was not an instance of AcceptableMediaType already
conversionApplied = _value != value;
result.add(_value);
} else {
conversionApplied = true;
result.addAll(HttpHeaderReader.readAcceptMediaType(HeaderUtils.asString(value, rd)));
}
} catch (java.text.ParseException e) {
throw exception(HttpHeaders.ACCEPT, value, e);
}
}
if (conversionApplied) {
// cache converted
headers.put(HttpHeaders.ACCEPT, result.stream().map((Function<MediaType, Object>) mediaType -> mediaType).collect(Collectors.toList()));
}
return Collections.unmodifiableList(result);
}
use of javax.ws.rs.core.MediaType in project jersey by jersey.
the class MessageBodyFactory method getMessageBodyWriterMediaTypes.
@Override
@SuppressWarnings("unchecked")
public List<MediaType> getMessageBodyWriterMediaTypes(final Class<?> c, final Type t, final Annotation[] as) {
final Set<MediaType> writeableMediaTypes = new LinkedHashSet<>();
for (final WriterModel model : writers) {
boolean writeableWorker = false;
for (final MediaType mt : model.declaredTypes()) {
if (model.isWriteable(c, t, as, mt)) {
writeableMediaTypes.add(mt);
writeableWorker = true;
}
if (!writeableMediaTypes.contains(MediaType.WILDCARD_TYPE) && writeableWorker && model.declaredTypes().contains(MediaType.WILDCARD_TYPE)) {
writeableMediaTypes.add(MediaType.WILDCARD_TYPE);
}
}
}
final List<MediaType> mtl = new ArrayList<>(writeableMediaTypes);
Collections.sort(mtl, MediaTypes.PARTIAL_ORDER_COMPARATOR);
return mtl;
}
Aggregations