use of javax.ws.rs.core.MultivaluedMap in project camel by apache.
the class TelegramServiceRestBotAPIAdapter method buildTextPart.
private Attachment buildTextPart(String name, String value) {
MultivaluedMap m = new MultivaluedHashMap<>();
m.putSingle("Content-Type", "text/plain");
m.putSingle("Content-Disposition", "form-data; name=\"" + escapeMimeName(name) + "\"");
Attachment a = new Attachment(m, value);
return a;
}
use of javax.ws.rs.core.MultivaluedMap in project jersey by jersey.
the class ResponseWriter method writeResponseStatusAndHeaders.
@Override
public OutputStream writeResponseStatusAndHeaders(final long contentLength, final ContainerResponse responseContext) throws ContainerException {
this.responseContext.complete(responseContext);
// first set the content length, so that if headers have an explicit value, it takes precedence over this one
if (responseContext.hasEntity() && contentLength != -1 && contentLength < Integer.MAX_VALUE) {
response.setContentLength((int) contentLength);
}
// Note that the writing of headers MUST be performed before
// the invocation of sendError as on some Servlet implementations
// modification of the response headers will have no effect
// after the invocation of sendError.
final MultivaluedMap<String, String> headers = getResponseContext().getStringHeaders();
for (final Map.Entry<String, List<String>> e : headers.entrySet()) {
final Iterator<String> it = e.getValue().iterator();
if (!it.hasNext()) {
continue;
}
final String header = e.getKey();
if (response.containsHeader(header)) {
// replace any headers previously set with values from Jersey container response.
response.setHeader(header, it.next());
}
while (it.hasNext()) {
response.addHeader(header, it.next());
}
}
final String reasonPhrase = responseContext.getStatusInfo().getReasonPhrase();
if (reasonPhrase != null) {
response.setStatus(responseContext.getStatus(), reasonPhrase);
} else {
response.setStatus(responseContext.getStatus());
}
if (!responseContext.hasEntity()) {
return null;
} else {
try {
final OutputStream outputStream = response.getOutputStream();
// so that any Servlet filters in the chain can still write to the response after us.
return new NonCloseableOutputStreamWrapper(outputStream);
} catch (final IOException e) {
throw new ContainerException(e);
}
}
}
use of javax.ws.rs.core.MultivaluedMap in project jersey by jersey.
the class MultiPartReaderClientSide method readMultiPart.
protected MultiPart readMultiPart(final Class<MultiPart> type, final Type genericType, final Annotation[] annotations, MediaType mediaType, final MultivaluedMap<String, String> headers, final InputStream stream) throws IOException, MIMEParsingException {
mediaType = unquoteMediaTypeParameters(mediaType, "boundary");
final MIMEMessage mimeMessage = new MIMEMessage(stream, mediaType.getParameters().get("boundary"), mimeConfig);
final boolean formData = MediaTypes.typeEqual(mediaType, MediaType.MULTIPART_FORM_DATA_TYPE);
final MultiPart multiPart = formData ? new FormDataMultiPart() : new MultiPart();
final MessageBodyWorkers workers = messageBodyWorkers.get();
multiPart.setMessageBodyWorkers(workers);
final MultivaluedMap<String, String> multiPartHeaders = multiPart.getHeaders();
for (final Map.Entry<String, List<String>> entry : headers.entrySet()) {
final List<String> values = entry.getValue();
for (final String value : values) {
multiPartHeaders.add(entry.getKey(), value);
}
}
final boolean fileNameFix;
if (!formData) {
multiPart.setMediaType(mediaType);
fileNameFix = false;
} else {
// see if the User-Agent header corresponds to some version of MS Internet Explorer
// if so, need to set fileNameFix to true to handle issue http://java.net/jira/browse/JERSEY-759
final String userAgent = headers.getFirst(HttpHeaders.USER_AGENT);
fileNameFix = userAgent != null && userAgent.contains(" MSIE ");
}
for (final MIMEPart mimePart : getMimeParts(mimeMessage)) {
final BodyPart bodyPart = formData ? new FormDataBodyPart(fileNameFix) : new BodyPart();
// Configure providers.
bodyPart.setMessageBodyWorkers(workers);
// Copy headers.
for (final Header header : mimePart.getAllHeaders()) {
bodyPart.getHeaders().add(header.getName(), header.getValue());
}
try {
final String contentType = bodyPart.getHeaders().getFirst("Content-Type");
if (contentType != null) {
bodyPart.setMediaType(MediaType.valueOf(contentType));
}
bodyPart.getContentDisposition();
} catch (final IllegalArgumentException ex) {
throw new BadRequestException(ex);
}
// Copy data into a BodyPartEntity structure.
bodyPart.setEntity(new BodyPartEntity(mimePart));
// Add this BodyPart to our MultiPart.
multiPart.getBodyParts().add(bodyPart);
}
return multiPart;
}
use of javax.ws.rs.core.MultivaluedMap in project jersey by jersey.
the class FeedEntriesAtomBodyWriter method writeTo.
@Override
public void writeTo(List<FeedEntry> entries, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
List<SyndEntry> syndEntries = entries.parallelStream().map(entry -> {
SyndContent description = new SyndContentImpl();
description.setType(MediaType.TEXT_PLAIN);
description.setValue(entry.getDescription());
SyndEntry syndEntry = new SyndEntryImpl();
syndEntry.setTitle(entry.getTitle());
syndEntry.setLink(entry.getLink());
syndEntry.setPublishedDate(entry.getPublishDate());
syndEntry.setDescription(description);
return syndEntry;
}).collect(Collectors.toList());
SyndFeed feed = new SyndFeedImpl();
feed.setFeedType("atom_1.0");
feed.setTitle("Combined Feed");
feed.setDescription("Combined Feed created by a feed-combiner application");
feed.setPublishedDate(new Date());
feed.setEntries(syndEntries);
writeSyndFeed(entityStream, feed);
}
use of javax.ws.rs.core.MultivaluedMap in project graylog2-server by Graylog2.
the class SessionAuthenticator method doGetAuthenticationInfo.
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
SessionIdToken sessionIdToken = (SessionIdToken) token;
final Subject subject = new Subject.Builder().sessionId(sessionIdToken.getSessionId()).buildSubject();
final Session session = subject.getSession(false);
if (session == null) {
LOG.debug("Invalid session {}. Either it has expired or did not exist.", sessionIdToken.getSessionId());
return null;
}
final Object username = subject.getPrincipal();
final User user = userService.load(String.valueOf(username));
if (user == null) {
LOG.debug("No user named {} found for session {}", username, sessionIdToken.getSessionId());
return null;
}
if (LOG.isDebugEnabled()) {
LOG.debug("Found session {} for user name {}", session.getId(), username);
}
@SuppressWarnings("unchecked") final MultivaluedMap<String, String> requestHeaders = (MultivaluedMap<String, String>) ThreadContext.get(ShiroSecurityContextFilter.REQUEST_HEADERS);
// extend session unless the relevant header was passed.
if (requestHeaders != null && !"true".equalsIgnoreCase(requestHeaders.getFirst(X_GRAYLOG_NO_SESSION_EXTENSION))) {
session.touch();
} else {
LOG.debug("Not extending session because the request indicated not to.");
}
ThreadContext.bind(subject);
return new SimpleAccount(user.getName(), null, "session authenticator");
}
Aggregations