001    /*
002     * The MIT License
003     * Copyright (c) 2012 Microsoft Corporation
004     *
005     * Permission is hereby granted, free of charge, to any person obtaining a copy
006     * of this software and associated documentation files (the "Software"), to deal
007     * in the Software without restriction, including without limitation the rights
008     * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
009     * copies of the Software, and to permit persons to whom the Software is
010     * furnished to do so, subject to the following conditions:
011     *
012     * The above copyright notice and this permission notice shall be included in
013     * all copies or substantial portions of the Software.
014     *
015     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
016     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
017     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
018     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
019     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
020     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
021     * THE SOFTWARE.
022     */
023    
024    package microsoft.exchange.webservices.data.core.request;
025    
026    import microsoft.exchange.webservices.data.core.ExchangeService;
027    import microsoft.exchange.webservices.data.core.enumeration.misc.TraceFlags;
028    import microsoft.exchange.webservices.data.core.exception.service.remote.ServiceRequestException;
029    import microsoft.exchange.webservices.data.misc.AsyncCallback;
030    import microsoft.exchange.webservices.data.misc.AsyncExecutor;
031    import microsoft.exchange.webservices.data.misc.AsyncRequestResult;
032    import microsoft.exchange.webservices.data.misc.CallableMethod;
033    import microsoft.exchange.webservices.data.misc.IAsyncResult;
034    
035    import java.io.IOException;
036    import java.util.concurrent.Callable;
037    import java.util.concurrent.Future;
038    
039    /**
040     * Defines the SimpleServiceRequestBase class.
041     */
042    public abstract class SimpleServiceRequestBase<T> extends ServiceRequestBase<T> {
043    
044      /**
045       * Initializes a new instance of the SimpleServiceRequestBase class.
046       */
047      protected SimpleServiceRequestBase(ExchangeService service)
048          throws Exception {
049        super(service);
050      }
051    
052      /**
053       * Executes this request.
054       *
055       * @return response object
056       * @throws Exception on error
057       */
058      protected T internalExecute() throws Exception {
059        HttpWebRequest response = null;
060    
061        try {
062          response = this.validateAndEmitRequest();
063          return this.readResponse(response);
064        } catch (IOException ex) {
065          // Wrap exception.
066          throw new ServiceRequestException(String.
067              format("The request failed. %s", ex.getMessage()), ex);
068        } catch (Exception e) {
069          if (response != null) {
070            this.getService().processHttpResponseHeaders(TraceFlags.
071                EwsResponseHttpHeaders, response);
072          }
073    
074          throw new ServiceRequestException(String.format("The request failed. %s", e.getMessage()), e);
075        }
076      }
077    
078      /**
079       * Ends executing this async request.
080       *
081       * @param asyncResult The async result
082       * @return Service response object
083       * @throws Exception on error
084       */
085      protected T endInternalExecute(IAsyncResult asyncResult) throws Exception {
086        HttpWebRequest response = (HttpWebRequest) asyncResult.get();
087        return this.readResponse(response);
088      }
089    
090      /**
091       * Begins executing this async request.
092       *
093       * @param callback The AsyncCallback delegate.
094       * @return An IAsyncResult that references the asynchronous request.
095       * @throws Exception on error
096       */
097      public AsyncRequestResult beginExecute(AsyncCallback callback) throws Exception {
098        this.validate();
099    
100        HttpWebRequest request = this.buildEwsHttpWebRequest();
101        AsyncExecutor es = new AsyncExecutor();
102        Callable<?> cl = new CallableMethod(request);
103        Future<?> task = es.submit(cl, callback);
104        es.shutdown();
105    
106        return new AsyncRequestResult(this, request, task, null);
107      }
108    
109    }