PageRenderTime 38ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/data-connector-agent/src/java/com/google/dataconnector/client/fetchrequest/URLConnectionStrategy.java

http://google-secure-data-connector.googlecode.com/
Java | 88 lines | 42 code | 11 blank | 35 comment | 2 complexity | d8bd92f9243f92ee2f81decf50f079b2 MD5 | raw file
Possible License(s): CPL-1.0, Apache-2.0, LGPL-3.0, JSON
  1. /* Copyright 2010 Google Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. *
  15. * $Id: URLConnectionStrategy.java 526 2010-02-18 23:00:24Z dchung@google.com $
  16. */
  17. package com.google.dataconnector.client.fetchrequest;
  18. import java.io.ByteArrayOutputStream;
  19. import java.io.IOException;
  20. import java.net.MalformedURLException;
  21. import java.net.URL;
  22. import java.net.URLConnection;
  23. import org.apache.log4j.Logger;
  24. import com.google.dataconnector.client.StrategyException;
  25. import com.google.dataconnector.client.FetchRequestHandler.Strategy;
  26. import com.google.dataconnector.protocol.proto.SdcFrame.FetchReply;
  27. import com.google.dataconnector.protocol.proto.SdcFrame.FetchRequest;
  28. import com.google.protobuf.ByteString;
  29. /**
  30. * Simple strategy of getting all the bytes from a URLConnection and send back
  31. * to the cloud.
  32. *
  33. * @author dchung
  34. *
  35. */
  36. public class URLConnectionStrategy implements Strategy {
  37. private static Logger LOG = Logger.getLogger(URLConnectionStrategy.class);
  38. public URLConnectionStrategy() {
  39. // Default constructor.
  40. }
  41. /**
  42. * Implements the strategy method of processing the request and filling in the
  43. * reply with results of processing.
  44. *
  45. * @param request The request.
  46. * @param reply The reply to fill in.
  47. */
  48. @Override
  49. public void process(FetchRequest request, FetchReply.Builder replyBuilder)
  50. throws StrategyException {
  51. try {
  52. // Perform the actual fetch here.
  53. URL resource = new URL(request.getResource());
  54. // Connect to resource.
  55. URLConnection conn = resource.openConnection();
  56. // Copy the result to reply buffer.
  57. ByteArrayOutputStream contents = new ByteArrayOutputStream();
  58. byte[] buffer = new byte[2048];
  59. int bytesRead = 0;
  60. int totalRead = 0;
  61. while ((bytesRead = conn.getInputStream().read(buffer)) > 0) {
  62. contents.write(buffer, 0, bytesRead);
  63. totalRead += bytesRead;
  64. }
  65. LOG.info("Read resource " + resource + ", bytes=" + totalRead);
  66. // finally set the content in the reply, if we have data
  67. if (totalRead > 0) {
  68. replyBuilder.setContents(ByteString.copyFrom(contents.toByteArray()));
  69. }
  70. replyBuilder.setStatus(0);
  71. } catch (MalformedURLException e) {
  72. throw new StrategyException(request.getId() + ": bad url.", e);
  73. } catch (IOException e) {
  74. throw new StrategyException(request.getId() + ": io exception.", e);
  75. }
  76. }
  77. }