我有一个非常大的Jetty应用程序。 我需要将所有302重定向替换为301。
在执行response.sendRedirect('someURL')时,我可以强制Jetty始终执行301重定向吗?
答案 0 :(得分:1)
码头6和8的黑客攻击:
if (server.getClass().getPackage().getImplementationVersion().startsWith("8")) {
try {
// Force 302 redirect status code into 301 'permanent redirect'
Field statusField = HttpGenerator.class.getDeclaredField("__status");
statusField.setAccessible(true);
Object[] statusMap = (Object[]) statusField.get(HttpStatus.class);
statusMap[HttpStatus.MOVED_TEMPORARILY_302] = statusMap[HttpStatus.MOVED_PERMANENTLY_301];
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
} else if (server.getClass().getPackage().getImplementationVersion().startsWith("6")) {
try {
// Force 302 redirect status code into 301 'permanent redirect'
Field statusField = HttpStatus.class
.getDeclaredField("__responseLine");
statusField.setAccessible(true);
Buffer[] responseLine = (Buffer[]) statusField
.get(HttpStatus.class);
byte[] bytes = responseLine[302].toString().replace("302", "301")
.getBytes();
responseLine[302] = new ByteArrayBuffer(bytes, 0, bytes.length,
Buffer.IMMUTABLE);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
} else {
throw new IllegalStateException("Jetty version "
+ server.getClass().getPackage().getImplementationVersion()
+ " not yet supported?");
}