~tritone-team/tritone/eucalyptus

« back to all changes in this revision

Viewing changes to clc/modules/wsstack/src/main/java/com/eucalyptus/ws/handlers/WalrusRESTBinding.java

  • Committer: Bazaar Package Importer
  • Author(s): Dustin Kirkland
  • Date: 2009-12-01 21:09:28 UTC
  • mto: This revision was merged to the branch mainline in revision 75.
  • Revision ID: james.westby@ubuntu.com-20091201210928-o2dvg0ubljhb0ft6
Tags: upstream-1.6.1~bzr1083
ImportĀ upstreamĀ versionĀ 1.6.1~bzr1083

Show diffs side-by-side

added added

removed removed

Lines of Context:
64
64
import java.lang.reflect.Field;
65
65
import java.lang.reflect.Modifier;
66
66
import java.lang.reflect.ParameterizedType;
 
67
import java.text.ParseException;
67
68
import java.util.ArrayList;
68
69
import java.util.Date;
69
70
import java.util.HashMap;
82
83
import org.apache.xml.dtm.ref.DTMNodeList;
83
84
import org.jboss.netty.buffer.ChannelBuffer;
84
85
import org.jboss.netty.buffer.ChannelBuffers;
 
86
import org.jboss.netty.channel.ChannelEvent;
85
87
import org.jboss.netty.channel.ChannelHandlerContext;
 
88
import org.jboss.netty.channel.Channels;
86
89
import org.jboss.netty.channel.DownstreamMessageEvent;
87
90
import org.jboss.netty.channel.MessageEvent;
88
91
import org.jboss.netty.handler.codec.http.DefaultHttpResponse;
95
98
import com.eucalyptus.auth.User;
96
99
import com.eucalyptus.auth.util.Hashes;
97
100
import com.eucalyptus.util.HoldMe;
 
101
import com.eucalyptus.util.LogUtil;
98
102
import com.eucalyptus.util.StorageProperties;
99
103
import com.eucalyptus.util.WalrusProperties;
100
104
import com.eucalyptus.ws.BindingException;
122
126
import edu.ucsb.eucalyptus.util.WalrusDataMessage;
123
127
import edu.ucsb.eucalyptus.util.WalrusDataMessenger;
124
128
import groovy.lang.GroovyObject;
 
129
import org.apache.commons.httpclient.util.DateUtil;
125
130
 
126
131
public class WalrusRESTBinding extends RestfulMarshallingHandler {
127
132
        private static Logger LOG = Logger.getLogger( WalrusRESTBinding.class );
132
137
        private static WalrusDataMessenger putMessenger;
133
138
        private static WalrusDataMessenger getMessenger;
134
139
        public static final int DATA_MESSAGE_SIZE = 102400;
 
140
        private String key;
 
141
        private String randomKey;
135
142
        private LinkedBlockingQueue<WalrusDataMessage> putQueue;
136
143
 
 
144
        @Override
 
145
        public void handleUpstream( final ChannelHandlerContext channelHandlerContext, final ChannelEvent channelEvent ) throws Exception {
 
146
                LOG.trace( LogUtil.dumpObject( channelEvent ) );
 
147
                if ( channelEvent instanceof MessageEvent ) {
 
148
                        final MessageEvent msgEvent = ( MessageEvent ) channelEvent;
 
149
                        try {
 
150
                                this.incomingMessage( channelHandlerContext, msgEvent );
 
151
                        } catch ( Throwable e ) {
 
152
                                LOG.error( e, e );
 
153
                                Channels.fireExceptionCaught( channelHandlerContext, e );
 
154
                                return;
 
155
                        }
 
156
                } else if (channelEvent.toString().contains("DISCONNECTED") ||
 
157
                                channelEvent.toString().contains("CLOSED")) {
 
158
                        if(key != null && randomKey != null) {
 
159
                                putMessenger.removeQueue(key, randomKey);
 
160
                                putQueue = null;
 
161
                        }
 
162
                }
 
163
                channelHandlerContext.sendUpstream( channelEvent );
 
164
        }
 
165
 
137
166
        @Override
138
167
        public void incomingMessage( ChannelHandlerContext ctx, MessageEvent event ) throws Exception {
139
168
                if ( event.getMessage( ) instanceof MappingHttpRequest ) {
407
436
                                        if(formFields.containsKey(WalrusProperties.CONTENT_TYPE)) {
408
437
                                                operationParams.put("ContentType", formFields.get(WalrusProperties.CONTENT_TYPE));
409
438
                                        }
410
 
                                        String key = target[0] + "." + objectKey;
411
 
                                        String randomKey = key + "." + Hashes.getRandom(10);
 
439
                                        key = target[0] + "." + objectKey;
 
440
                                        randomKey = key + "." + Hashes.getRandom(10);
412
441
                                        if(contentLengthString != null)
413
442
                                                operationParams.put("ContentLength", (new Long(contentLength).toString()));
414
443
                                        operationParams.put(WalrusProperties.Headers.RandomKey.toString(), randomKey);
477
506
 
478
507
                                        } else {
479
508
                                                //handle PUTs
480
 
                                                String key = target[0] + "." + objectKey;
481
 
                                                String randomKey = key + "." + Hashes.getRandom(10);
 
509
                                                key = target[0] + "." + objectKey;
 
510
                                                randomKey = key + "." + Hashes.getRandom(10);
482
511
                                                String contentType = httpRequest.getHeader(WalrusProperties.CONTENT_TYPE);
483
512
                                                if(contentType != null)
484
513
                                                        operationParams.put("ContentType", contentType);
594
623
                return operationName;   
595
624
        }
596
625
 
597
 
        private void parseExtendedHeaders(Map operationParams, String headerString, String value) {
 
626
        private void parseExtendedHeaders(Map operationParams, String headerString, String value) throws BindingException {
598
627
                if(headerString.equals(WalrusProperties.ExtendedGetHeaders.Range.toString())) {
599
628
                        String prefix = "bytes=";
600
629
                        assert(value.startsWith(prefix));
610
639
                        operationParams.put(WalrusProperties.ExtendedHeaderRangeTypes.ByteRangeEnd.toString(), Long.parseLong(values[1]));
611
640
                } else if(WalrusProperties.ExtendedHeaderDateTypes.contains(headerString)) {
612
641
                        try {
613
 
                                operationParams.put(headerString, DateUtils.parseIso8601DateTimeOrDate(value));
 
642
                                List<String> dateFormats = new ArrayList<String>();
 
643
                                dateFormats.add(DateUtil.PATTERN_RFC1123);
 
644
                                operationParams.put(headerString, DateUtil.parseDate(value, dateFormats));
614
645
                        } catch(Exception ex) {
615
 
                                ex.printStackTrace();
 
646
                                try {
 
647
                                        operationParams.put(headerString, DateUtils.parseIso8601DateTime(value));
 
648
                                } catch (ParseException e) {
 
649
                                        LOG.error(e);
 
650
                                        throw new BindingException(e);
 
651
                                }
616
652
                        }
617
653
                } else {
618
654
                        operationParams.put(headerString, value);
635
671
                                ArrayList<Grant> grants = new ArrayList<Grant>();
636
672
 
637
673
                                List<String> permissions = xmlParser.getValues("//AccessControlList/Grant/Permission");
 
674
                                if(permissions == null)
 
675
                                        throw new BindingException("malformed access control list");
638
676
 
639
677
                                DTMNodeList grantees = xmlParser.getNodes("//AccessControlList/Grant/Grantee");
640
 
 
 
678
                                if(grantees == null)
 
679
                                        throw new BindingException("malformed access control list");
641
680
 
642
681
                                for(int i = 0 ; i < grantees.getLength() ; ++i) {
643
682
                                        String id = xmlParser.getValue(grantees.item(i), "ID");
685
724
                                ArrayList<Grant> grants = new ArrayList<Grant>();
686
725
 
687
726
                                List<String> permissions = xmlParser.getValues("/AccessControlList/Grant/Permission");
 
727
                                if(permissions == null)
 
728
                                        throw new BindingException("malformed access control list");
688
729
 
689
730
                                DTMNodeList grantees = xmlParser.getNodes("/AccessControlList/Grant/Grantee");
 
731
                                if(grantees == null)
 
732
                                        throw new BindingException("malformed access control list");
690
733
 
691
734
 
692
735
                                for(int i = 0 ; i < grantees.getLength() ; ++i) {