~ubuntu-branches/ubuntu/raring/eucalyptus/raring

« back to all changes in this revision

Viewing changes to clc/modules/wsstack/src/main/java/com/eucalyptus/ws/server/MetadataPipeline.java

  • Committer: Package Import Robot
  • Author(s): Brian Thomason
  • Date: 2011-11-29 13:17:52 UTC
  • mfrom: (1.2.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 185.
  • Revision ID: package-import@ubuntu.com-20111129131752-rq31al3ntutv2vvl
Tags: upstream-3.0.999beta1
ImportĀ upstreamĀ versionĀ 3.0.999beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
package com.eucalyptus.ws.server;
2
2
 
3
3
import java.net.InetSocketAddress;
4
 
import java.util.List;
5
 
 
 
4
import java.util.NoSuchElementException;
6
5
import org.apache.log4j.Logger;
7
6
import org.jboss.netty.buffer.ChannelBuffer;
8
7
import org.jboss.netty.buffer.ChannelBuffers;
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;
22
 
 
 
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;
29
30
 
30
31
@ChannelPipelineCoverage( "one" )
31
 
public class MetadataPipeline extends FilteredPipeline implements UnrollableStage, ChannelUpstreamHandler {
32
 
  private static Logger LOG = Logger.getLogger( MetadataPipeline.class );
33
 
  @Override
34
 
  protected void addStages( List<UnrollableStage> stages ) {
35
 
    stages.add( this );
36
 
  }
37
 
 
38
 
  @Override
39
 
  protected boolean checkAccepts( HttpRequest message ) {
40
 
    return message.getUri( ).matches("/latest(/.*)*") || message.getUri( ).matches("/\\d\\d\\d\\d-\\d\\d-\\d\\d/.*");
41
 
  }
42
 
 
43
 
  @Override
44
 
  public String getPipelineName( ) {
45
 
    return "instance-metadata";
46
 
  }
47
 
 
48
 
  @Override
49
 
  public String getStageName( ) {
50
 
    return "instance-metadata";
51
 
  }
52
 
 
53
 
  @Override
54
 
  public void unrollStage( ChannelPipeline pipeline ) {
55
 
    pipeline.addLast( "instance-metadata", this );
56
 
  }
57
 
 
 
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" +
 
38
                                             " <head>\n" +
 
39
                                             "  <title>404 - Not Found</title>\n" +
 
40
                                             " </head>\n" +
 
41
                                             " <body>\n" +
 
42
                                             "  <h1>404 - Not Found: failed to find %s</h1>\n" +
 
43
                                             "  <pre>%s</pre>\n" +
 
44
                                             " </body>\n" +
 
45
                                             "</html>\n";
 
46
  private static Logger       LOG          = Logger.getLogger( MetadataPipeline.class );
 
47
  
 
48
  public MetadataPipeline( ) {
 
49
    super( );
 
50
  }
 
51
  
 
52
  @Override
 
53
  public boolean checkAccepts( HttpRequest message ) {
 
54
    return message.getUri( ).matches( "/latest(/.*)*" ) || message.getUri( ).matches( "/\\d\\d\\d\\d-\\d\\d-\\d\\d/.*" );
 
55
  }
 
56
  
 
57
  @Override
 
58
  public String getName( ) {
 
59
    return "metadata-pipeline";
 
60
  }
 
61
  
58
62
  @Override
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 + ":" );
68
 
      else
69
 
        newUri = uri.replaceAll( "/\\d\\d\\d\\d-\\d\\d-\\d\\d/", remoteHost + ":" );
70
 
 
 
72
      else newUri = uri.replaceAll( "/\\d\\d\\d\\d-\\d\\d-\\d\\d/", remoteHost + ":" );
 
73
      
71
74
      HttpResponse response = null;
72
 
      LOG.info( "Trying to get metadata: " + newUri );
73
 
      Object reply = null;
 
75
      LOG.trace( "Trying to get metadata: " + newUri );
 
76
      Object reply = "".getBytes( );
 
77
      Exception replyEx = null;
74
78
      try {
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( );
 
83
        } else {
 
84
          reply = ServiceContext.send( "VmMetadata", newUri );
 
85
        }
 
86
      } catch ( ServiceDispatchException e1 ) {
 
87
        Logs.extreme( ).debug( e1, e1 );
 
88
        replyEx = e1;
76
89
      } catch ( Exception e1 ) {
77
 
        LOG.debug( e1, e1 );
 
90
        Logs.extreme( ).debug( e1, e1 );
 
91
        replyEx = e1;
78
92
      } finally {
79
93
        Contexts.clear( request.getCorrelationId( ) );
80
94
      }
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(
 
99
          ERROR_STRING,
 
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 );
 
107
        } else {
 
108
          buffer = ChannelBuffers.wrappedBuffer( errorMessage.getBytes( ) );
 
109
          response.setContent( buffer );
 
110
        }
 
111
        response.addHeader( HttpHeaders.Names.CONTENT_LENGTH, Integer.toString( buffer.readableBytes( ) ) );
 
112
      } else {
 
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( ) ) );
87
118
      }
88
 
      else
89
 
        response = new DefaultHttpResponse(request.getProtocolVersion( ),HttpResponseStatus.NOT_FOUND);
90
119
      ctx.getChannel( ).write( response ).addListener( ChannelFutureListener.CLOSE );
91
120
    } else {
92
121
      ctx.sendUpstream( e );
93
122
    }
94
123
  }
95
 
 
 
124
  
 
125
  @Override
 
126
  public ChannelPipeline addHandlers( ChannelPipeline pipeline ) {
 
127
    pipeline.addLast( "instance-metadata", this );
 
128
    return pipeline;
 
129
  }
 
130
  
96
131
}