| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
package com.thindeck.cockpit; |
| 31 | |
|
| 32 | |
import com.jcabi.log.VerboseProcess; |
| 33 | |
import com.jcabi.manifests.Manifests; |
| 34 | |
import com.thindeck.api.Base; |
| 35 | |
import com.thindeck.cockpit.deck.TkDeck; |
| 36 | |
import java.io.File; |
| 37 | |
import java.io.IOException; |
| 38 | |
import java.nio.charset.Charset; |
| 39 | |
import java.util.regex.Pattern; |
| 40 | |
import org.takes.Take; |
| 41 | |
import org.takes.facets.auth.PsByFlag; |
| 42 | |
import org.takes.facets.flash.TkFlash; |
| 43 | |
import org.takes.facets.fork.FkAnonymous; |
| 44 | |
import org.takes.facets.fork.FkAuthenticated; |
| 45 | |
import org.takes.facets.fork.FkFixed; |
| 46 | |
import org.takes.facets.fork.FkHitRefresh; |
| 47 | |
import org.takes.facets.fork.FkParams; |
| 48 | |
import org.takes.facets.fork.FkRegex; |
| 49 | |
import org.takes.facets.fork.TkFork; |
| 50 | |
import org.takes.facets.forward.TkForward; |
| 51 | |
import org.takes.tk.TkClasspath; |
| 52 | |
import org.takes.tk.TkFiles; |
| 53 | |
import org.takes.tk.TkMeasured; |
| 54 | |
import org.takes.tk.TkRedirect; |
| 55 | |
import org.takes.tk.TkVersioned; |
| 56 | |
import org.takes.tk.TkWithHeaders; |
| 57 | |
import org.takes.tk.TkWithType; |
| 58 | |
import org.takes.tk.TkWrap; |
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
public final class TkApp extends TkWrap { |
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | 1 | private static final String VERSION = String.format( |
| 76 | |
"%s %s %s", |
| 77 | |
|
| 78 | |
Manifests.read("Thindeck-Version"), |
| 79 | |
Manifests.read("Thindeck-Revision"), |
| 80 | |
Manifests.read("Thindeck-Date") |
| 81 | |
); |
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
public TkApp(final Base base) throws IOException { |
| 89 | 1 | super(TkApp.make(base)); |
| 90 | 1 | } |
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
private static Take make(final Base base) throws IOException { |
| 99 | 1 | if (!"UTF-8".equals(Charset.defaultCharset().name())) { |
| 100 | 0 | throw new IllegalStateException( |
| 101 | |
String.format( |
| 102 | |
"default encoding is %s", Charset.defaultCharset() |
| 103 | |
) |
| 104 | |
); |
| 105 | |
} |
| 106 | 1 | return new TkWithHeaders( |
| 107 | |
new TkVersioned( |
| 108 | |
new TkMeasured( |
| 109 | |
new TkFlash( |
| 110 | |
new TkAppFallback( |
| 111 | |
new TkForward( |
| 112 | |
new TkAppAuth( |
| 113 | |
TkApp.regex(base) |
| 114 | |
) |
| 115 | |
) |
| 116 | |
) |
| 117 | |
) |
| 118 | |
) |
| 119 | |
), |
| 120 | |
String.format("X-Thindeck-Version: %s", TkApp.VERSION), |
| 121 | |
"Vary: Cookie" |
| 122 | |
); |
| 123 | |
} |
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
private static Take regex(final Base base) throws IOException { |
| 132 | 1 | return new TkFork( |
| 133 | |
new FkParams( |
| 134 | |
PsByFlag.class.getSimpleName(), |
| 135 | |
Pattern.compile(".+"), |
| 136 | |
new TkRedirect() |
| 137 | |
), |
| 138 | |
new FkRegex("/robots.txt", ""), |
| 139 | |
new FkRegex( |
| 140 | |
"/xsl/[a-z\\-]+\\.xsl", |
| 141 | |
new TkWithType( |
| 142 | |
TkApp.refresh("./src/main/xsl"), |
| 143 | |
"text/xsl" |
| 144 | |
) |
| 145 | |
), |
| 146 | |
new FkRegex( |
| 147 | |
"/js/[a-z]+\\.js", |
| 148 | |
new TkWithType( |
| 149 | |
TkApp.refresh("./src/main/js"), |
| 150 | |
"text/javascript" |
| 151 | |
) |
| 152 | |
), |
| 153 | |
new FkRegex( |
| 154 | |
"/css/[a-z]+\\.css", |
| 155 | |
new TkWithType( |
| 156 | |
TkApp.refresh("./src/main/scss"), |
| 157 | |
"text/css" |
| 158 | |
) |
| 159 | |
), |
| 160 | |
new FkAnonymous( |
| 161 | |
new TkFork( |
| 162 | |
new FkRegex("/", new TkIndex(base)) |
| 163 | |
) |
| 164 | |
), |
| 165 | |
new FkAuthenticated( |
| 166 | |
new TkFork( |
| 167 | |
new FkRegex("/", new TkDecks(base)), |
| 168 | |
new FkRegex("/acc", new TkAccount(base)), |
| 169 | |
new FkRegex("/add", new TkAddDeck(base)), |
| 170 | |
new FkRegex("/d/.*", new TkDeck(base)) |
| 171 | |
) |
| 172 | |
) |
| 173 | |
); |
| 174 | |
} |
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
@SuppressWarnings("PMD.DoNotUseThreads") |
| 183 | |
private static Take refresh(final String path) throws IOException { |
| 184 | 3 | return new TkFork( |
| 185 | |
new FkHitRefresh( |
| 186 | |
new File(path), |
| 187 | 3 | new Runnable() { |
| 188 | |
@Override |
| 189 | |
public void run() { |
| 190 | 0 | new VerboseProcess( |
| 191 | |
new ProcessBuilder( |
| 192 | |
"mvn", |
| 193 | |
"generate-resources" |
| 194 | |
) |
| 195 | |
).stdout(); |
| 196 | 0 | } |
| 197 | |
}, |
| 198 | |
new TkFiles("./target/classes") |
| 199 | |
), |
| 200 | |
new FkFixed(new TkClasspath()) |
| 201 | |
); |
| 202 | |
} |
| 203 | |
|
| 204 | |
} |