|
| 1 | +/** |
| 2 | + * Jooby https://jooby.io |
| 3 | + * Apache License Version 2.0 https://jooby.io/LICENSE.txt |
| 4 | + * Copyright 2014 Edgar Espina |
| 5 | + */ |
| 6 | +package io.jooby.internal; |
| 7 | + |
| 8 | +import io.jooby.AttachedFile; |
| 9 | +import io.jooby.Context; |
| 10 | +import io.jooby.DefaultContext; |
| 11 | +import io.jooby.ForwardingContext; |
| 12 | +import io.jooby.MediaType; |
| 13 | +import io.jooby.MessageEncoder; |
| 14 | +import io.jooby.Route; |
| 15 | +import io.jooby.Sender; |
| 16 | +import io.jooby.SneakyThrows; |
| 17 | +import io.jooby.StatusCode; |
| 18 | +import org.jetbrains.annotations.NotNull; |
| 19 | + |
| 20 | +import javax.annotation.Nonnull; |
| 21 | +import java.io.IOException; |
| 22 | +import java.io.InputStream; |
| 23 | +import java.io.OutputStream; |
| 24 | +import java.io.PrintWriter; |
| 25 | +import java.nio.ByteBuffer; |
| 26 | +import java.nio.channels.FileChannel; |
| 27 | +import java.nio.channels.ReadableByteChannel; |
| 28 | +import java.nio.charset.Charset; |
| 29 | +import java.nio.charset.StandardCharsets; |
| 30 | +import java.nio.file.Files; |
| 31 | +import java.nio.file.Path; |
| 32 | + |
| 33 | +public class HeadContext extends ForwardingContext { |
| 34 | + /** |
| 35 | + * Creates a new forwarding context. |
| 36 | + * |
| 37 | + * @param context Source context. |
| 38 | + */ |
| 39 | + public HeadContext(@Nonnull Context context) { |
| 40 | + super(context); |
| 41 | + } |
| 42 | + |
| 43 | + @Nonnull @Override public Context send(@Nonnull Path file) { |
| 44 | + try { |
| 45 | + ctx.setResponseLength(Files.size(file)); |
| 46 | + ctx.setResponseType(MediaType.byFile(file)); |
| 47 | + ctx.send(StatusCode.OK); |
| 48 | + return this; |
| 49 | + } catch (IOException x) { |
| 50 | + throw SneakyThrows.propagate(x); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + @Nonnull @Override public Context send(@Nonnull byte[] data) { |
| 55 | + ctx.setResponseLength(data.length); |
| 56 | + ctx.send(StatusCode.OK); |
| 57 | + return this; |
| 58 | + } |
| 59 | + |
| 60 | + @Nonnull @Override public Context send(@Nonnull String data) { |
| 61 | + return send(data, StandardCharsets.UTF_8); |
| 62 | + } |
| 63 | + |
| 64 | + @Nonnull @Override public Context send(@Nonnull ByteBuffer data) { |
| 65 | + ctx.setResponseLength(data.remaining()); |
| 66 | + ctx.send(StatusCode.OK); |
| 67 | + return this; |
| 68 | + } |
| 69 | + |
| 70 | + @Nonnull @Override public Context send(@Nonnull FileChannel file) { |
| 71 | + try { |
| 72 | + ctx.setResponseLength(file.size()); |
| 73 | + ctx.send(StatusCode.OK); |
| 74 | + return this; |
| 75 | + } catch (IOException x) { |
| 76 | + throw SneakyThrows.propagate(x); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + @Nonnull @Override public Context send(@Nonnull AttachedFile file) { |
| 81 | + ctx.setResponseLength(file.getFileSize()); |
| 82 | + ctx.setResponseType(file.getContentType()); |
| 83 | + ctx.send(StatusCode.OK); |
| 84 | + return this; |
| 85 | + } |
| 86 | + |
| 87 | + @Nonnull @Override public Context send(@Nonnull InputStream input) { |
| 88 | + ctx.setResponseHeader("Transfer-Encoding", "chunked"); |
| 89 | + ctx.send(input); |
| 90 | + ctx.send(StatusCode.OK); |
| 91 | + return this; |
| 92 | + } |
| 93 | + |
| 94 | + @Nonnull @Override public Context send(@Nonnull StatusCode statusCode) { |
| 95 | + ctx.send(statusCode); |
| 96 | + return this; |
| 97 | + } |
| 98 | + |
| 99 | + @Nonnull @Override public Context send(@Nonnull ReadableByteChannel channel) { |
| 100 | + ctx.setResponseHeader("Transfer-Encoding", "chunked"); |
| 101 | + ctx.send(StatusCode.OK); |
| 102 | + return this; |
| 103 | + } |
| 104 | + |
| 105 | + @Nonnull @Override public Context send(@Nonnull String data, @Nonnull Charset charset) { |
| 106 | + ctx.setResponseLength(data.getBytes(charset).length); |
| 107 | + ctx.send(StatusCode.OK); |
| 108 | + return this; |
| 109 | + } |
| 110 | + |
| 111 | + @Nonnull @Override public Context render(@Nonnull Object value) { |
| 112 | + try { |
| 113 | + Route route = getRoute(); |
| 114 | + MessageEncoder encoder = route.getEncoder(); |
| 115 | + byte[] bytes = encoder.encode(this, value); |
| 116 | + if (bytes == null) { |
| 117 | + if (!isResponseStarted()) { |
| 118 | + throw new IllegalStateException("The response was not encoded"); |
| 119 | + } |
| 120 | + } else { |
| 121 | + send(bytes); |
| 122 | + } |
| 123 | + return this; |
| 124 | + } catch (Exception x) { |
| 125 | + throw SneakyThrows.propagate(x); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + @Nonnull @Override public Sender responseSender() { |
| 130 | + ctx.setResponseHeader("Transfer-Encoding", "chunked"); |
| 131 | + ctx.send(StatusCode.OK); |
| 132 | + return new NoopSender(); |
| 133 | + } |
| 134 | + |
| 135 | + @Nonnull @Override public OutputStream responseStream() { |
| 136 | + ctx.setResponseHeader("Transfer-Encoding", "chunked"); |
| 137 | + ctx.send(StatusCode.OK); |
| 138 | + return new NoopOutputStream(); |
| 139 | + } |
| 140 | + |
| 141 | + @Nonnull @Override public PrintWriter responseWriter() { |
| 142 | + return new PrintWriter(responseStream()); |
| 143 | + } |
| 144 | + |
| 145 | + private static class NoopOutputStream extends OutputStream { |
| 146 | + @Override public void write(@NotNull byte[] b) throws IOException { |
| 147 | + } |
| 148 | + |
| 149 | + @Override public void write(@NotNull byte[] b, int off, int len) throws IOException { |
| 150 | + } |
| 151 | + |
| 152 | + @Override public void write(int b) throws IOException { |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + private static class NoopSender implements Sender { |
| 157 | + @Nonnull @Override public Sender write(@Nonnull byte[] data, @Nonnull Callback callback) { |
| 158 | + return this; |
| 159 | + } |
| 160 | + |
| 161 | + @Override public void close() { |
| 162 | + } |
| 163 | + } |
| 164 | +} |
0 commit comments