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
024package microsoft.exchange.webservices.data.misc;
025
026import microsoft.exchange.webservices.data.core.EwsUtilities;
027import microsoft.exchange.webservices.data.core.ExchangeService;
028import microsoft.exchange.webservices.data.core.request.HttpWebRequest;
029import microsoft.exchange.webservices.data.core.request.ServiceRequestBase;
030import microsoft.exchange.webservices.data.core.request.SimpleServiceRequestBase;
031import microsoft.exchange.webservices.data.core.request.WaitHandle;
032import microsoft.exchange.webservices.data.core.exception.misc.ArgumentException;
033
034import java.util.concurrent.ExecutionException;
035import java.util.concurrent.Future;
036import java.util.concurrent.FutureTask;
037import java.util.concurrent.TimeUnit;
038import java.util.concurrent.TimeoutException;
039
040public class AsyncRequestResult implements IAsyncResult {
041
042  ServiceRequestBase serviceRequest;
043  HttpWebRequest webRequest;
044  AsyncCallback wasasyncCallback;
045  IAsyncResult webAsyncResult;
046  Object asyncState;
047  Future<?> task;
048
049  AsyncRequestResult(Future<?> task) {
050    this.task = task;
051  }
052
053
054  public AsyncRequestResult(ServiceRequestBase serviceRequest,
055      HttpWebRequest webRequest, Future<?> task,
056      Object asyncState) throws Exception {
057    EwsUtilities.validateParam(serviceRequest, "serviceRequest");
058    EwsUtilities.validateParam(webRequest, "webRequest");
059    EwsUtilities.validateParam(task, "task");
060    this.serviceRequest = serviceRequest;
061    this.webRequest = webRequest;
062    this.asyncState = asyncState;
063    this.task = task;
064
065  }
066
067  public void setServiceRequestBase(ServiceRequestBase serviceRequest) {
068    this.serviceRequest = serviceRequest;
069  }
070
071  private ServiceRequestBase getServiceRequest() {
072    return this.serviceRequest;
073  }
074
075  public void setHttpWebRequest(HttpWebRequest webRequest) {
076    this.webRequest = webRequest;
077  }
078
079  public HttpWebRequest getHttpWebRequest() {
080    return this.webRequest;
081  }
082
083  public FutureTask<?> getTask() {
084    return (FutureTask<?>) this.task;
085  }
086
087  public static <T extends SimpleServiceRequestBase> T extractServiceRequest(
088      ExchangeService exchangeService, Future<?> asyncResult) throws Exception {
089    EwsUtilities.validateParam(asyncResult, "asyncResult");
090    AsyncRequestResult asyncRequestResult = (AsyncRequestResult) asyncResult;
091    if (asyncRequestResult == null) {
092      /**
093       * String.InvalidAsyncResult is copied from the error message
094       * HttpWebRequest.EndGetResponse() Just use this simple string for
095       * all kinds of invalid IAsyncResult parameters.
096       */
097      throw new ArgumentException("Invalid AsyncResult.",
098          "asyncResult");
099    }
100    // Validate the serivce request.
101    if (asyncRequestResult.serviceRequest == null) {
102      throw new ArgumentException("Invalid AsyncResult.",
103          "asyncResult");
104    }
105    // Validate the service object
106    if (!asyncRequestResult.serviceRequest.getService().equals(
107        exchangeService)) {
108      throw new ArgumentException("Invalid AsyncResult.",
109          "asyncResult");
110    }
111    T serviceRequest = (T) asyncRequestResult.getServiceRequest();
112    // Validate the request type
113    if (serviceRequest == null) {
114      throw new ArgumentException("Invalid AsyncResult.",
115          "asyncResult");
116    }
117    return serviceRequest;
118
119  }
120
121
122  @Override
123  public boolean cancel(boolean arg0) {
124    // TODO Auto-generated method stub
125    return false;
126  }
127
128
129
130  @Override
131  public Object get(long timeout, TimeUnit unit)
132      throws InterruptedException, ExecutionException,
133      TimeoutException {
134    // TODO Auto-generated method stub
135    return null;
136  }
137
138
139  @Override
140  public boolean isCancelled() {
141    // TODO Auto-generated method stub
142    return false;
143  }
144
145
146  @Override
147  public boolean isDone() {
148    // TODO Auto-generated method stub
149    return false;
150  }
151
152
153  @Override
154  public Object getAsyncState() {
155    // TODO Auto-generated method stub
156    return null;
157  }
158
159
160  @Override
161  public WaitHandle getAsyncWaitHanle() {
162    // TODO Auto-generated method stub
163    return null;
164  }
165
166
167  @Override
168  public boolean getCompleteSynchronously() {
169    // TODO Auto-generated method stub
170    return false;
171  }
172
173
174  @Override
175  public boolean getIsCompleted() {
176    // TODO Auto-generated method stub
177    return false;
178  }
179
180
181  @Override
182  public Object get() throws InterruptedException, ExecutionException {
183    // TODO Auto-generated method stub
184    return this.task.get();
185  }
186
187
188
189}