本文整理汇总了Java中org.takes.rq.RqHeaders类的典型用法代码示例。如果您正苦于以下问题:Java RqHeaders类的具体用法?Java RqHeaders怎么用?Java RqHeaders使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
RqHeaders类属于org.takes.rq包,在下文中一共展示了RqHeaders类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: route
import org.takes.rq.RqHeaders; //导入依赖的package包/类
@Override
public Opt<Response> route(final Request req) throws IOException {
final Iterator<String> headers =
new RqHeaders.Base(req).header("Accept-Encoding").iterator();
final Opt<Response> resp;
if (this.encoding.isEmpty()) {
resp = new Opt.Single<Response>(this.origin);
} else if (headers.hasNext()) {
final Collection<String> items = Arrays.asList(
ENCODING_SEP.split(
new EnglishLowerCase(headers.next().trim())
.string()
)
);
if (items.contains(this.encoding)) {
resp = new Opt.Single<Response>(this.origin);
} else {
resp = new Opt.Empty<Response>();
}
} else {
resp = new Opt.Empty<Response>();
}
return resp;
}
示例2: route
import org.takes.rq.RqHeaders; //导入依赖的package包/类
@Override
public Opt<Response> route(final Request req) throws IOException {
final Iterator<String> header =
new RqHeaders.Base(req).header("X-Takes-HitRefresh").iterator();
final Opt<Response> resp;
if (header.hasNext()) {
if (this.expired()) {
this.exec.run();
FkHitRefresh.touch(this.last);
}
resp = new Opt.Single<Response>(this.take.act(req));
} else {
resp = new Opt.Empty<Response>();
}
return resp;
}
示例3: fork
import org.takes.rq.RqHeaders; //导入依赖的package包/类
/**
* Make fork.
* @param host Host
* @param take Take to use
* @return Fork
*/
private static Fork fork(final String host, final Take take) {
return new Fork() {
@Override
public Opt<Response> route(final Request req) throws IOException {
final String hst = new RqHeaders.Smart(
new RqHeaders.Base(req)
).single("host");
final Opt<Response> rsp;
if (new EnglishLowerCase(host).string()
.equals(new EnglishLowerCase(hst).string())) {
rsp = new Opt.Single<>(take.act(req));
} else {
rsp = new Opt.Empty<>();
}
return rsp;
}
};
}
示例4: asMap
import org.takes.rq.RqHeaders; //导入依赖的package包/类
/**
* Convert a list of requests to a map.
* @param reqs Requests
* @return Map of them
* @throws IOException If fails
*/
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
private static Map<String, List<Request>> asMap(
final Collection<Request> reqs) throws IOException {
final Map<String, List<Request>> map = new HashMap<>(reqs.size());
for (final Request req : reqs) {
final String header =
new RqHeaders.Smart(req).single("Content-Disposition");
final Matcher matcher = RqMtBase.NAME.matcher(header);
if (!matcher.matches()) {
throw new HttpException(
HttpURLConnection.HTTP_BAD_REQUEST,
String.format(
"\"name\" not found in Content-Disposition header: %s",
header
)
);
}
final String name = matcher.group(1);
if (!map.containsKey(name)) {
map.put(name, new LinkedList<Request>());
}
map.get(name).add(req);
}
return map;
}
示例5: logsUserIn
import org.takes.rq.RqHeaders; //导入依赖的package包/类
/**
* TkAuth can login a user.
* @throws IOException If some problem inside
*/
@Test
public void logsUserIn() throws IOException {
final Pass pass = new PsFixed(new Identity.Simple("urn:test:1"));
final Take take = Mockito.mock(Take.class);
Mockito.doReturn(new RsText()).when(take)
.act(Mockito.any(Request.class));
new TkAuth(take, pass).act(new RqFake());
final ArgumentCaptor<Request> captor =
ArgumentCaptor.forClass(Request.class);
Mockito.verify(take).act(captor.capture());
MatcherAssert.assertThat(
new RqHeaders.Base(captor.getValue()).header(
TkAuth.class.getSimpleName()
),
Matchers.hasItem("urn%3Atest%3A1")
);
}
示例6: logsUserOut
import org.takes.rq.RqHeaders; //导入依赖的package包/类
/**
* TkAuth can logout a user.
* @throws IOException If some problem inside
*/
@Test
public void logsUserOut() throws IOException {
final Pass pass = new PsLogout();
final Take take = Mockito.mock(Take.class);
Mockito.doReturn(new RsText()).when(take)
.act(Mockito.any(Request.class));
new TkAuth(take, pass).act(
new RqWithHeader(
new RqFake(),
TkAuth.class.getSimpleName(),
"urn%3Atest%3A2"
)
);
final ArgumentCaptor<Request> captor =
ArgumentCaptor.forClass(Request.class);
Mockito.verify(take).act(captor.capture());
MatcherAssert.assertThat(
new RqHeaders.Base(captor.getValue()).header(
TkAuth.class.getSimpleName()
),
Matchers.emptyIterable()
);
}
示例7: deck
import org.takes.rq.RqHeaders; //导入依赖的package包/类
/**
* Get deck.
* @return The deck
* @throws IOException If fails
*/
@SuppressWarnings("PMD.PreserveStackTrace")
public Deck deck() throws IOException {
final User user = new RqUser(this, this.base).get();
final String name = new RqHeaders.Smart(
new RqHeaders.Base(this)
).single("X-Thindeck-Deck");
try {
return user.decks().get(name);
} catch (final IOException ex) {
throw new RsForward(
new RsText(ex.getLocalizedMessage()),
HttpURLConnection.HTTP_NOT_FOUND
);
}
}
示例8: accepted
import org.takes.rq.RqHeaders; //导入依赖的package包/类
/**
* Get all types accepted by the client.
* @param req Request
* @return Media types
* @throws IOException If fails
*/
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
private static MediaTypes accepted(final Request req) throws IOException {
MediaTypes list = new MediaTypes();
final Iterable<String> headers = new RqHeaders.Base(req)
.header("Accept");
for (final String hdr : headers) {
list = list.merge(new MediaTypes(hdr));
}
if (list.isEmpty()) {
list = new MediaTypes("text/html");
}
return list;
}
示例9: getType
import org.takes.rq.RqHeaders; //导入依赖的package包/类
/**
* Get Content-Type type provided by the client.
* @param req Request
* @return Media type
* @throws IOException If fails
*/
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
private static MediaTypes getType(final Request req) throws IOException {
MediaTypes list = new MediaTypes();
final Iterable<String> headers = new RqHeaders.Base(req)
.header("Content-Type");
for (final String hdr : headers) {
list = list.merge(new MediaTypes(hdr));
}
if (list.isEmpty()) {
list = new MediaTypes("*/*");
}
return list;
}
示例10: identity
import org.takes.rq.RqHeaders; //导入依赖的package包/类
/**
* Authenticated user.
* @return User identity
* @throws IOException If fails
*/
public Identity identity() throws IOException {
final Iterator<String> headers =
new RqHeaders.Base(this).header(this.header).iterator();
final Identity user;
if (headers.hasNext()) {
user = new CcPlain().decode(
new Utf8String(headers.next()).bytes()
);
} else {
user = Identity.ANONYMOUS;
}
return user;
}
示例11: request
import org.takes.rq.RqHeaders; //导入依赖的package包/类
/**
* Creates the request to be forwarded to the target host.
*
* @param req Original request
* @param dest Destination URL
* @return Request to be forwarded
* @throws IOException If some problem inside
*/
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
private com.jcabi.http.Request request(final Request req,
final URI dest) throws IOException {
final String method = new RqMethod.Base(req).method();
com.jcabi.http.Request proxied = new JdkRequest(dest).method(method);
final RqHeaders headers = new RqHeaders.Base(req);
for (final String name : headers.names()) {
if ("content-length".equals(new EnglishLowerCase(name).string())) {
continue;
}
if (TkProxy.isHost(name)) {
proxied = proxied.header(name, this.target);
continue;
}
for (final String value : headers.header(name)) {
proxied = proxied.header(name, value);
}
}
if (headers.header("Content-Length").iterator().hasNext()
|| headers.header("Transfer-Encoding").iterator().hasNext()) {
final ByteArrayOutputStream output = new ByteArrayOutputStream();
new RqPrint(new RqLengthAware(req)).printBody(output);
proxied = proxied.body().set(output.toByteArray()).back();
}
return proxied;
}
示例12: fakeBody
import org.takes.rq.RqHeaders; //导入依赖的package包/类
/**
* Fake body creator.
* @param parts Fake request body parts
* @return StringBuilder of given dispositions
* @throws IOException If fails
*/
@SuppressWarnings(
{
"PMD.InsufficientStringBufferDeclaration",
"PMD.AvoidInstantiatingObjectsInLoops"
})
private static StringBuilder fakeBody(final Request... parts)
throws IOException {
final StringBuilder builder = new StringBuilder();
for (final Request part : parts) {
builder.append(String.format("--%s", RqMtFake.BOUNDARY))
.append(RqMtFake.CRLF)
.append("Content-Disposition: ")
.append(
new RqHeaders.Smart(
new RqHeaders.Base(part)
).single("Content-Disposition")
).append(RqMtFake.CRLF);
final String body = new RqPrint(part).printBody();
if (!(RqMtFake.CRLF.equals(body) || body.isEmpty())) {
builder.append(RqMtFake.CRLF)
.append(body)
.append(RqMtFake.CRLF);
}
}
builder.append("Content-Transfer-Encoding: utf-8")
.append(RqMtFake.CRLF)
.append(String.format("--%s--", RqMtFake.BOUNDARY));
return builder;
}
示例13: RqDisposition
import org.takes.rq.RqHeaders; //导入依赖的package包/类
/**
* Ctor.
* @param req Request.
* @throws IOException In case of error.
*/
public RqDisposition(final Request req) throws IOException {
super(req);
this.content = new RqHeaders.Smart(
new RqHeaders.Base(req)
).single("Content-Disposition");
}
示例14: make
import org.takes.rq.RqHeaders; //导入依赖的package包/类
/**
* Make it.
* @param xsl XSL
* @param base Base
* @param req Request
* @param src Source
* @return Response
* @throws IOException If fails
* @checkstyle ParameterNumberCheck (5 lines)
*/
private static Response make(final String xsl, final Base base,
final Request req, final XeSource... src) throws IOException {
final Response xbl = new RsXembly(
new XeStylesheet(xsl),
new XePage(base, req, src)
);
final Response raw = new RsWithType(xbl, "text/xml");
return new RsFork(
req,
new Fork() {
@Override
public Opt<Response> route(final Request rst)
throws IOException {
final RqHeaders hdr = new RqHeaders.Base(rst);
final Iterator<String> agent =
hdr.header("User-Agent").iterator();
final Opt<Response> opt;
if (agent.hasNext() && agent.next().contains("Firefox")) {
opt = new Opt.Single<Response>(
// @checkstyle MultipleStringLiteralsCheck (1 line)
new RsXSLT(new RsWithType(raw, "text/html"))
);
} else {
opt = new Opt.Empty<>();
}
return opt;
}
},
new FkTypes("application/xml,text/xml", new RsPrettyXML(raw)),
new FkTypes(
"*/*",
new RsXSLT(new RsWithType(raw, "text/html"))
)
);
}
示例15: enter
import org.takes.rq.RqHeaders; //导入依赖的package包/类
@Override
public Opt<Identity> enter(final Request req) throws IOException {
// @checkstyle ExecutableStatementCount (100 lines)
final Iterator<String> headers = new RqHeaders.Base(req)
.header(this.header).iterator();
Opt<Identity> user = new Opt.Empty<>();
String head = "";
while (headers.hasNext()) {
final String nexthead = headers.next();
if (nexthead.trim().startsWith("Bearer")) {
head = nexthead;
break;
}
}
final String dot = "\\.";
if (head.length() > 0) {
final String jwt = head.split(" ", 2)[1].trim();
final String[] parts = jwt.split(dot);
final byte[] jwtheader = parts[0].getBytes();
final byte[] jwtpayload = parts[1].getBytes();
final byte[] jwtsign = parts[2].getBytes();
final ByteBuffer tocheck = ByteBuffer.allocate(
jwtheader.length + jwtpayload.length + 1
);
tocheck.put(jwtheader).put(".".getBytes()).put(jwtpayload);
final byte[] checked = this.signature.sign(tocheck.array());
if (Arrays.equals(jwtsign, checked)) {
try (final JsonReader rdr = Json.createReader(
new StringReader(
new String(new Base64().decode(jwtpayload))
)
)) {
user = new Opt.Single<Identity>(
new Identity.Simple(
rdr.readObject().getString(Token.Jwt.SUBJECT)
)
);
}
}
}
return user;
}