19
18
import org.jboss.netty.handler.codec.http.HttpResponse;
20
19
import org.jboss.netty.handler.codec.http.HttpResponseStatus;
21
20
import org.mule.transport.NullPayload;
21
import com.eucalyptus.bootstrap.Bootstrap;
22
import com.eucalyptus.component.ComponentId.ComponentPart;
23
import com.eucalyptus.component.id.Eucalyptus;
23
24
import com.eucalyptus.context.Contexts;
24
import com.eucalyptus.context.NoSuchContextException;
25
25
import com.eucalyptus.context.ServiceContext;
26
import com.eucalyptus.context.ServiceDispatchException;
26
27
import com.eucalyptus.http.MappingHttpRequest;
27
import com.eucalyptus.ws.stages.UnrollableStage;
28
import com.eucalyptus.ws.util.Messaging;
28
import com.eucalyptus.records.Logs;
29
import com.eucalyptus.util.Exceptions;
30
31
@ChannelPipelineCoverage( "one" )
31
public class MetadataPipeline extends FilteredPipeline implements UnrollableStage, ChannelUpstreamHandler {
32
private static Logger LOG = Logger.getLogger( MetadataPipeline.class );
34
protected void addStages( List<UnrollableStage> stages ) {
39
protected boolean checkAccepts( HttpRequest message ) {
40
return message.getUri( ).matches("/latest(/.*)*") || message.getUri( ).matches("/\\d\\d\\d\\d-\\d\\d-\\d\\d/.*");
44
public String getPipelineName( ) {
45
return "instance-metadata";
49
public String getStageName( ) {
50
return "instance-metadata";
54
public void unrollStage( ChannelPipeline pipeline ) {
55
pipeline.addLast( "instance-metadata", this );
32
@ComponentPart( Eucalyptus.class )
33
public class MetadataPipeline extends FilteredPipeline implements ChannelUpstreamHandler {
34
private static final String ERROR_STRING = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n" +
35
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n" +
36
" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n" +
37
"<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n" +
39
" <title>404 - Not Found</title>\n" +
42
" <h1>404 - Not Found: failed to find %s</h1>\n" +
46
private static Logger LOG = Logger.getLogger( MetadataPipeline.class );
48
public MetadataPipeline( ) {
53
public boolean checkAccepts( HttpRequest message ) {
54
return message.getUri( ).matches( "/latest(/.*)*" ) || message.getUri( ).matches( "/\\d\\d\\d\\d-\\d\\d-\\d\\d/.*" );
58
public String getName( ) {
59
return "metadata-pipeline";
59
63
public void handleUpstream( ChannelHandlerContext ctx, ChannelEvent e ) throws Exception {
60
if( e instanceof MessageEvent && ( (MessageEvent ) e).getMessage( ) instanceof MappingHttpRequest ) {
61
MappingHttpRequest request = ( MappingHttpRequest ) ((MessageEvent) e).getMessage( );
64
if ( e instanceof MessageEvent && ( ( MessageEvent ) e ).getMessage( ) instanceof MappingHttpRequest ) {
65
MappingHttpRequest request = ( MappingHttpRequest ) ( ( MessageEvent ) e ).getMessage( );
62
66
String newUri = null;
63
67
String uri = request.getUri( );
64
InetSocketAddress remoteAddr = ((InetSocketAddress) ctx.getChannel( ).getRemoteAddress( ) );
68
InetSocketAddress remoteAddr = ( ( InetSocketAddress ) ctx.getChannel( ).getRemoteAddress( ) );
65
69
String remoteHost = remoteAddr.getAddress( ).getHostAddress( );//"10.1.1.2";//
66
if( uri.startsWith( "/latest/" ) )
70
if ( uri.startsWith( "/latest/" ) )
67
71
newUri = uri.replaceAll( "/latest/", remoteHost + ":" );
69
newUri = uri.replaceAll( "/\\d\\d\\d\\d-\\d\\d-\\d\\d/", remoteHost + ":" );
72
else newUri = uri.replaceAll( "/\\d\\d\\d\\d-\\d\\d-\\d\\d/", remoteHost + ":" );
71
74
HttpResponse response = null;
72
LOG.info( "Trying to get metadata: " + newUri );
75
LOG.trace( "Trying to get metadata: " + newUri );
76
Object reply = "".getBytes( );
77
Exception replyEx = null;
75
reply = ServiceContext.send( "VmMetadata", newUri );
79
if ( Bootstrap.isShuttingDown( ) ) {
80
reply = "System shutting down".getBytes( );
81
} else if ( !Bootstrap.isFinished( ) ) {
82
reply = "System is still starting up".getBytes( );
84
reply = ServiceContext.send( "VmMetadata", newUri );
86
} catch ( ServiceDispatchException e1 ) {
87
Logs.extreme( ).debug( e1, e1 );
76
89
} catch ( Exception e1 ) {
90
Logs.extreme( ).debug( e1, e1 );
79
93
Contexts.clear( request.getCorrelationId( ) );
81
if ( reply != null && !( reply instanceof NullPayload ) ) {
82
response = new DefaultHttpResponse(request.getProtocolVersion( ),HttpResponseStatus.OK);
83
response.setHeader( HttpHeaders.Names.CONTENT_TYPE, "text/html" );
95
Logs.extreme( ).debug( "VmMetadata reply info: " + reply + " " + replyEx );
96
if ( replyEx != null || reply == null || reply instanceof NullPayload ) {
97
response = new DefaultHttpResponse( request.getProtocolVersion( ), HttpResponseStatus.NOT_FOUND );
98
String errorMessage = String.format(
100
newUri.replaceAll( remoteHost + ":", "" ),
101
replyEx != null && Logs.isDebug( ) ? Exceptions.string( replyEx ) : "" );
102
response.setHeader( HttpHeaders.Names.CONTENT_TYPE, "text/plain" );
103
ChannelBuffer buffer = null;
104
if ( replyEx != null && !( replyEx instanceof NoSuchElementException ) ) {
105
buffer = ChannelBuffers.wrappedBuffer( errorMessage.getBytes( ) );
106
response.setContent( buffer );
108
buffer = ChannelBuffers.wrappedBuffer( errorMessage.getBytes( ) );
109
response.setContent( buffer );
111
response.addHeader( HttpHeaders.Names.CONTENT_LENGTH, Integer.toString( buffer.readableBytes( ) ) );
113
response = new DefaultHttpResponse( request.getProtocolVersion( ), HttpResponseStatus.OK );
114
response.setHeader( HttpHeaders.Names.CONTENT_TYPE, "text/plain" );
84
115
ChannelBuffer buffer = ChannelBuffers.wrappedBuffer( ( byte[] ) reply );
85
116
response.setContent( buffer );
86
117
response.addHeader( HttpHeaders.Names.CONTENT_LENGTH, Integer.toString( buffer.readableBytes( ) ) );
89
response = new DefaultHttpResponse(request.getProtocolVersion( ),HttpResponseStatus.NOT_FOUND);
90
119
ctx.getChannel( ).write( response ).addListener( ChannelFutureListener.CLOSE );
92
121
ctx.sendUpstream( e );
126
public ChannelPipeline addHandlers( ChannelPipeline pipeline ) {
127
pipeline.addLast( "instance-metadata", this );