Reputation: 517
I have Jetty application which is very big. I need to replace all 302 redirects to 301.
Can I force Jetty to always do 301 redirect when doing response.sendRedirect('someURL') ?
Upvotes: 0
Views: 859
Reputation: 61
Hacks for jetty 6 and 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?");
}
Upvotes: 1