Пример #1
0
 /** Opens a connection based on the requets object */
 public HTTPConnectionImpl openRequestConnection(final Request request) throws IOException {
   this.connect(request);
   this.updateCookies(request);
   this.request = request;
   if (this.doRedirects && request.getLocation() != null) {
     if (request.getLocation().toLowerCase().startsWith("ftp://")) {
       throw new BrowserException("Cannot redirect to FTP");
     }
     final String org = request.getUrl();
     final String red = request.getLocation();
     if (org.equalsIgnoreCase(red) && this.redirectLoopCounter >= 20) {
       final Logger llogger = this.getLogger();
       if (llogger != null) {
         llogger.severe("20 Redirects!!!");
       }
     } else if (!org.equalsIgnoreCase(red) || this.redirectLoopCounter < 20) {
       if (org.equalsIgnoreCase(red)) {
         this.redirectLoopCounter++;
       } else {
         this.redirectLoopCounter = 0;
       }
       /* prevent buggy redirect loops */
       /* source==dest */
       try {
         /* close old connection, because we follow redirect */
         request.httpConnection.disconnect();
       } catch (final Throwable e) {
       }
       this.openGetConnection(null);
     }
   } else {
     this.currentURL = request.getUrl();
   }
   return this.request.getHttpConnection();
 }
Пример #2
0
  private String createUrlWithParams(final Request<?> request) {
    if (request == null || TextUtils.isEmpty(request.getUrl())) {
      return null;
    }
    Uri.Builder uriBuilder = Uri.parse(request.getUrl()).buildUpon();

    List<Parameter> params = request.getUrlParams();
    if (checkListOfParams(params)) {
      for (Parameter parameter : params) {
        uriBuilder.appendQueryParameter(parameter.getName(), parameter.getValue());
      }
    }
    return uriBuilder.build().toString();
  }
  void prepareRequest(HttpURLConnection connection, Request request) throws IOException {
    // HttpURLConnection artificially restricts request method
    try {
      connection.setRequestMethod(request.getMethod());
    } catch (ProtocolException e) {
      try {
        methodField.set(connection, request.getMethod());
      } catch (IllegalAccessException e1) {
        throw RetrofitError.unexpectedError(request.getUrl(), e1);
      }
    }

    connection.setDoInput(true);

    for (Header header : request.getHeaders()) {
      connection.addRequestProperty(header.getName(), header.getValue());
    }

    TypedOutput body = request.getBody();
    if (body != null) {
      connection.setDoOutput(true);
      connection.addRequestProperty("Content-Type", body.mimeType());
      long length = body.length();
      if (length != -1) {
        connection.setFixedLengthStreamingMode((int) length);
        connection.addRequestProperty("Content-Length", String.valueOf(length));
      } else {
        connection.setChunkedStreamingMode(CHUNK_SIZE);
      }
      body.writeTo(connection.getOutputStream());
    }
  }
 public IRequestHandler mapRequest(final Request request) {
   final Url url = this.decryptUrl(request, request.getUrl());
   if (url == null) {
     return this.wrappedMapper.mapRequest(request);
   }
   return this.wrappedMapper.mapRequest(request.cloneWithUrl(url));
 }
Пример #5
0
 public void setRequest(final Request request) {
   if (request == null) {
     return;
   }
   this.updateCookies(request);
   this.request = request;
   this.currentURL = request.getUrl();
 }
Пример #6
0
  @JsonIgnore
  public String getProxyUrl() {
    if (browserProxyUrl != null) {
      return browserProxyUrl;
    }

    return proxyBaseUrl + originalRequest.getUrl();
  }
  /**
   * dismissReminderRaw listenable future.
   *
   * @return the listenable future
   */
  public ListenableFuture<String> dismissReminderRaw() {

    Request request = getResolver().createRequest();
    request.setVerb(HttpVerb.POST);

    request.getUrl().appendPathComponent("microsoft.graph.dismissReminder");

    ListenableFuture<OrcResponse> future = oDataExecute(request);
    return transformToStringListenableFuture(future);
  }
Пример #8
0
 protected void setupRequestHeader() {
   Header header = request.getHeader();
   if (null != header) {
     for (String key : header.keys()) {
       conn.setRequestProperty(key, header.get(key));
     }
   }
   URL url = request.getUrl();
   conn.setRequestProperty("host", url.getHost() + ":" + url.getPort());
 }
Пример #9
0
 public void updateCookies(final Request request) {
   if (request == null) {
     return;
   }
   final String host = Browser.getHost(request.getUrl());
   Cookies cookies = this.getCookies().get(host);
   if (cookies == null) {
     cookies = new Cookies();
     this.getCookies().put(host, cookies);
   }
   cookies.add(request.getCookies());
 }
Пример #10
0
 protected HttpURLConnection openConnection(Request request) throws IOException {
   HttpURLConnection connection = (HttpURLConnection) new URL(request.getUrl()).openConnection();
   connection.setConnectTimeout(Defaults.CONNECT_TIMEOUT_MILLIS);
   connection.setReadTimeout(Defaults.READ_TIMEOUT_MILLIS);
   connection.addRequestProperty("Cache-Control", "no-cache");
   connection.setUseCaches(false);
   if (isAndroid) { // 解决android中的EOFException
     if (Build.VERSION.SDK_INT > 13) {
       connection.setRequestProperty("Connection", "close"); // sdk3.2之后用这个
     } else {
       connection.setRequestProperty("http.keepAlive", "false"); // sdk2.2之前用这个
     }
   }
   return connection;
 }
Пример #11
0
  private static synchronized void waitForPageAccess(final Browser browser, final Request request)
      throws InterruptedException {
    final String host = Browser.getHost(request.getUrl());
    try {
      Integer localLimit = null;
      Integer globalLimit = null;
      Long localLastRequest = null;
      Long globalLastRequest = null;

      if (localLimit == null && globalLimit == null) {
        return;
      }
      if (localLastRequest == null && globalLastRequest == null) {
        return;
      }
      if (localLimit != null && localLastRequest == null) {
        return;
      }
      if (globalLimit != null && globalLastRequest == null) {
        return;
      }

      if (globalLimit == null) {
        globalLimit = 0;
      }
      if (localLimit == null) {
        localLimit = 0;
      }
      if (localLastRequest == null) {
        localLastRequest = System.currentTimeMillis();
      }
      if (globalLastRequest == null) {
        globalLastRequest = System.currentTimeMillis();
      }
      final long dif =
          Math.max(
              localLimit - (System.currentTimeMillis() - localLastRequest),
              globalLimit - (System.currentTimeMillis() - globalLastRequest));

      if (dif > 0) {
        // System.out.println("Sleep " + dif + " before connect to " +
        // request.getUrl().getHost());
        Thread.sleep(dif);
        // waitForPageAccess(request);
      }
    } finally {
    }
  }
  /**
   * search listenable future.
   *
   * @param q the q
   * @return the listenable future
   */
  public ListenableFuture<DriveItem> search(String q) {

    java.util.Map<String, Object> map = new java.util.HashMap<String, Object>();
    map.put("q", q);

    Request request = getResolver().createRequest();
    request.setVerb(HttpVerb.GET);
    String parameters = getFunctionParameters(map);

    request.getUrl().appendPathComponent("microsoft.graph.search(" + parameters + ")");

    ListenableFuture<OrcResponse> future = oDataExecute(request);

    return transformToEntityListenableFuture(
        transformToStringListenableFuture(future), DriveItem.class, getResolver());
  }
  /**
   * restoreRaw listenable future.
   *
   * @param identifierUris the identifierUris
   * @return the listenable future
   */
  public ListenableFuture<String> restoreRaw(String identifierUris) {

    java.util.Map<String, String> map = new java.util.HashMap<String, String>();

    map.put("identifierUris", identifierUris);

    Request request = getResolver().createRequest();
    request.setVerb(HttpVerb.POST);

    request.setContent(
        getResolver().getJsonSerializer().jsonObjectFromJsonMap(map).getBytes(Constants.UTF8));

    request.getUrl().appendPathComponent("Microsoft.Graph.restore");

    ListenableFuture<OrcResponse> future = oDataExecute(request);
    return transformToStringListenableFuture(future);
  }
  /**
   * checkMemberGroupsRaw listenable future.
   *
   * @param groupIds the groupIds
   * @return the listenable future
   */
  public ListenableFuture<String> checkMemberGroupsRaw(String groupIds) {

    java.util.Map<String, String> map = new java.util.HashMap<String, String>();

    map.put("groupIds", groupIds);

    Request request = getResolver().createRequest();
    request.setVerb(HttpVerb.POST);

    request.setContent(
        getResolver().getJsonSerializer().jsonObjectFromJsonMap(map).getBytes(Constants.UTF8));

    request.getUrl().appendPathComponent("Microsoft.DirectoryServices.checkMemberGroups");

    ListenableFuture<OrcResponse> future = oDataExecute(request);
    return transformToStringListenableFuture(future);
  }
  /**
   * snoozeReminderRaw listenable future.
   *
   * @param newReminderTime the newReminderTime
   * @return the listenable future
   */
  public ListenableFuture<String> snoozeReminderRaw(String newReminderTime) {

    java.util.Map<String, String> map = new java.util.HashMap<String, String>();

    map.put("NewReminderTime", newReminderTime);

    Request request = getResolver().createRequest();
    request.setVerb(HttpVerb.POST);

    request.setContent(
        getResolver().getJsonSerializer().jsonObjectFromJsonMap(map).getBytes(Constants.UTF8));

    request.getUrl().appendPathComponent("microsoft.graph.snoozeReminder");

    ListenableFuture<OrcResponse> future = oDataExecute(request);
    return transformToStringListenableFuture(future);
  }
  /**
   * tentativelyAcceptRaw listenable future.
   *
   * @param comment the comment @param sendResponse the sendResponse
   * @return the listenable future
   */
  public ListenableFuture<String> tentativelyAcceptRaw(String comment, String sendResponse) {

    java.util.Map<String, String> map = new java.util.HashMap<String, String>();

    map.put("Comment", comment);
    map.put("SendResponse", sendResponse);

    Request request = getResolver().createRequest();
    request.setVerb(HttpVerb.POST);

    request.setContent(
        getResolver().getJsonSerializer().jsonObjectFromJsonMap(map).getBytes(Constants.UTF8));

    request.getUrl().appendPathComponent("microsoft.graph.tentativelyAccept");

    ListenableFuture<OrcResponse> future = oDataExecute(request);
    return transformToStringListenableFuture(future);
  }
Пример #17
0
  public void forwardCookies(final Request request) {
    if (request == null) {
      return;
    }
    final String host = Browser.getHost(request.getUrl());
    final Cookies cookies = this.getCookies().get(host);
    if (cookies == null) {
      return;
    }

    for (final Cookie cookie : cookies.getCookies()) {
      // Pfade sollten verarbeitet werden...TODO
      if (cookie.isExpired()) {
        continue;
      }
      request.getCookies().add(cookie);
    }
  }
Пример #18
0
  private Response doMultipart(final Request<?> request) {
    if (checkRequest(request)) {
      return null;
    }
    HttpPost post = new HttpPost(request.getUrl());
    MultipartEntity entity = new MultipartEntity();
    if (request.getFile() != null) {
      // TO DO: create an object for multipart entity;
      entity.addPart("file", new FileBody(request.getFile()));
      try {
        entity.addPart("file_name", new StringBody(request.getFile().getName()));
      } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
      }
      post.setEntity(entity);
    } else if (request.getFiles() != null) {
      File[] files = request.getFiles();
      for (int i = 0; i < files.length; i++) {
        entity.addPart("file" + i, new FileBody(files[i]));
      }
      post.setEntity(entity);
    }

    //        List<Parameter> postEntities = request.getPostEntities();
    //        if (checkListOfParams(postEntities)) {
    //            final LinkedList<BasicNameValuePair> postParams = new
    // LinkedList<BasicNameValuePair>();
    //            for (Parameter parameter : postEntities) {
    //                postParams.add(new BasicNameValuePair(parameter.getName(),
    // parameter.getValue()));
    //            }
    //            try {
    //                //Encoding parameter
    //                post.setEntity(new UrlEncodedFormEntity(postParams, "UTF-8"));
    //            } catch (final UnsupportedEncodingException ex) {
    //                return buildErrorResponse(ex.getMessage(), ResponseStatus.FAILED);
    //            }
    //        }
    return doRequest(request, post);
  }
 protected HttpURLConnection openConnection(Request request) throws IOException {
   HttpURLConnection connection = (HttpURLConnection) new URL(request.getUrl()).openConnection();
   connection.setConnectTimeout(Defaults.CONNECT_TIMEOUT_MILLIS);
   connection.setReadTimeout(Defaults.READ_TIMEOUT_MILLIS);
   return connection;
 }
  public Request extractRequestFromBloaStatus(ConnManager.UserStatus status) {
    final String statusText = status.getText();
    Request request = new Request();

    // SR, SF, TF, LG
    Matcher matcher = requestPattern.matcher(statusText);
    if (matcher.find()) {

      final String requestText = matcher.group();

      request.setQualifier(requestText.substring(0, 2));
      request.setCompleteRequestText(requestText);

      matcher = userPattern.matcher(requestText);
      if (matcher.find()) { // optional
        request.setAddressedUser(new String(matcher.group().substring(1)));
      }

      matcher = operationServicePattern.matcher(requestText);
      if (matcher.find()) {
        String[] operationService = matcher.group().trim().split("\\.");
        request.setOperation(operationService[0]);
        request.setService(operationService[1]);
      } else {
        return null;
      }

      matcher = urlPattern.matcher(requestText);
      if (matcher.find()) {
        request.setUrl(matcher.group());
      }

      matcher = queryStringPattern.matcher(requestText);
      if (matcher.find()) {
        String[] queryArguments = matcher.group().split("&");

        for (String arg : queryArguments) {
          String[] assignment = arg.split("=");
          request.getVariables().put(assignment[0], assignment[1]);
        }
      }

      if (request.getQualifier().equals("SR") || request.getQualifier().equals("SF")) {
        // only url or querystring allowed
        if (request.getUrl() == null && request.getVariables().size() == 0
            || request.getUrl() != null && request.getVariables().size() > 0) {
          return null;
        }
      }

      // e.g. [@ikangai.availability?=true]
      matcher = conditionPattern.matcher(requestText);
      if (matcher.find()) { // optional
        String[] condition = matcher.group().split("=");
        String[] userVariable = condition[0].split("\\.");

        Condition con =
            new Condition(
                userVariable[0].substring(2), // remove [@
                userVariable[1].substring(0, userVariable[1].length() - 1), // remove ?
                condition[1].substring(0, condition[1].length() - 1)); // remove ]
        request.setCondition(con);
      }

      matcher = hashTagPattern.matcher(requestText);
      // Find all matches
      while (matcher.find()) { // optional
        // Get the matching string
        String match = matcher.group();
        request.getHashTags().add(match);
      }

      request.setTweetId(status.getId());
      // TODO
      // request.setCreatedAt(status.getCreatedAt());
      request.setCreatedAt(new Date());
      // TODO
      // request.setRequester(status.getSender());
      request.setRequester("From me:)");

      return request;
    }

    // VA ExplicitVariableAssignment
    // VA varname value
    matcher = variableAssignmentPattern.matcher(statusText);
    if (matcher.find()) {
      final String requestText = matcher.group();

      request.setQualifier(requestText.substring(0, 2));
      request.setCompleteRequestText(requestText);

      String[] parts = requestText.split(" ");
      request.getVariables().put(parts[1], parts[2]);

      request.setTweetId(status.getId());
      // TODO
      // request.setCreatedAt(status.getCreatedAt());
      request.setCreatedAt(new Date());
      // TODO
      // request.setRequester(status.getSender());
      request.setRequester("From me:)");

      return request;
    }

    // @user.varname?
    // Access variable (will be created if not existent)
    matcher = variableAccessPattern.matcher(statusText);
    if (matcher.find()) {
      final String requestText = matcher.group();

      request.setQualifier("AccessVariable");
      request.setCompleteRequestText(requestText);

      String[] parts = requestText.split("\\.");
      request.setAddressedUser(new String(parts[0].substring(1))); // remove @
      request.getVariables().put(parts[1].substring(0, parts[1].length() - 1), null); // remove ?

      request.setTweetId(status.getId());
      // TODO
      // request.setCreatedAt(status.getCreatedAt());
      request.setCreatedAt(new Date());
      // TODO
      // request.setRequester(status.getSender());
      request.setRequester("From me:)");

      return request;
    }

    // @user.operation.Service?
    // Access service result (Implicit) / ask user to retweet result
    matcher = serviceResultAccessPattern.matcher(statusText);
    if (matcher.find()) {
      final String requestText = matcher.group();

      request.setQualifier("AccessServiceResult");
      request.setCompleteRequestText(requestText);

      String[] parts = requestText.split("\\.");
      request.setAddressedUser(new String(parts[0].substring(1))); // remove @
      request.setOperation(parts[1]);
      request.setService(parts[2].substring(0, parts[2].length() - 1)); // remove ?		

      request.setTweetId(status.getId());
      // TODO
      // request.setCreatedAt(status.getCreatedAt());
      request.setCreatedAt(new Date());
      // TODO
      // request.setRequester(status.getSender());
      request.setRequester("From me:)");

      return request;
    }

    return null;
  }
Пример #21
0
 protected void openConnection() throws IOException {
   conn = request.getUrl().openConnection();
   if (timeout > 0) conn.setReadTimeout(timeout);
 }
  @Override
  public <T> ListenableFuture<T> execute(Request request, AsyncHandler<T> handler)
      throws IOException {
    final Expectation expectation = expectations.get(new URL(request.getUrl()));
    if (expectation == null) {
      throw new RuntimeException("Unknown URL requested, failing test: " + request.getUrl());
    }
    fulfilledExpectations.add(expectation);
    T t = null;
    try {
      final URI uri = expectation.getUrl().toURI();
      handler.onStatusReceived(
          new HttpResponseStatus(uri, this) {
            @Override
            public int getStatusCode() {
              return expectation.getStatusCode();
            }

            @Override
            public String getStatusText() {
              return ""; // TODO
            }

            @Override
            public String getProtocolName() {
              return expectation.getUrl().getProtocol();
            }

            @Override
            public int getProtocolMajorVersion() {
              return 1;
            }

            @Override
            public int getProtocolMinorVersion() {
              return 1;
            }

            @Override
            public String getProtocolText() {
              return ""; // TODO
            }
          });
      handler.onHeadersReceived(
          new HttpResponseHeaders(uri, this) {
            @Override
            public FluentCaseInsensitiveStringsMap getHeaders() {
              return new FluentCaseInsensitiveStringsMap();
            }
          });
      handler.onBodyPartReceived(
          new HttpResponseBodyPart(uri, this) {
            @Override
            public byte[] getBodyPartBytes() {
              return expectation.getPayload().getBytes(Charset.forName("UTF-8"));
            }

            @Override
            public int writeTo(OutputStream outputStream) throws IOException {
              final byte[] bodyPartBytes = getBodyPartBytes();
              outputStream.write(bodyPartBytes);
              return bodyPartBytes.length;
            }

            @Override
            public ByteBuffer getBodyByteBuffer() {
              return ByteBuffer.wrap(getBodyPartBytes());
            }

            @Override
            public boolean isLast() {
              return true;
            }

            @Override
            public void markUnderlyingConnectionAsClosed() {}

            @Override
            public boolean closeUnderlyingConnection() {
              return true;
            }
          });
      t = handler.onCompleted();
    } catch (Exception e) {
      e
          .printStackTrace(); // To change body of catch statement use File | Settings | File
                              // Templates.
    }
    final T finalT = t;
    final Future<T> futureT =
        Executors.newSingleThreadExecutor()
            .submit(
                new Callable<T>() {
                  @Override
                  public T call() throws Exception {
                    return finalT;
                  }
                });
    return new ImmediateFuture<>(futureT);
  }
Пример #23
0
 private boolean checkRequest(final Request<?> request) {
   return request == null || TextUtils.isEmpty(request.getUrl());
 }