com.google.api.client.util.IOUtils Java Examples

The following examples show how to use com.google.api.client.util.IOUtils. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CustomDataStoreFactory.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
public final V get( String key ) throws IOException {
  if ( key == null ) {
    return null;
  } else {
    this.lock.lock();

    V var2;
    try {
      var2 = IOUtils.deserialize( (byte[]) this.keyValueMap.get( key ) );
    } finally {
      this.lock.unlock();
    }

    return var2;
  }
}
 
Example #2
Source File: CustomDataStoreFactory.java    From hop with Apache License 2.0 6 votes vote down vote up
public final Collection<V> values() throws IOException {
  this.lock.lock();

  try {
    List<V> result = Lists.newArrayList();
    Iterator i$ = this.keyValueMap.values().iterator();

    while ( i$.hasNext() ) {
      byte[] bytes = (byte[]) i$.next();
      result.add( IOUtils.deserialize( bytes ) );
    }

    List var7 = Collections.unmodifiableList( result );
    return var7;
  } finally {
    this.lock.unlock();
  }
}
 
Example #3
Source File: CustomDataStoreFactory.java    From hop with Apache License 2.0 6 votes vote down vote up
public final V get( String key ) throws IOException {
  if ( key == null ) {
    return null;
  } else {
    this.lock.lock();

    V var2;
    try {
      var2 = IOUtils.deserialize( (byte[]) this.keyValueMap.get( key ) );
    } finally {
      this.lock.unlock();
    }

    return var2;
  }
}
 
Example #4
Source File: CustomDataStoreFactory.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
public final Collection<V> values() throws IOException {
  this.lock.lock();

  try {
    List<V> result = Lists.newArrayList();
    Iterator i$ = this.keyValueMap.values().iterator();

    while ( i$.hasNext() ) {
      byte[] bytes = (byte[]) i$.next();
      result.add( IOUtils.deserialize( bytes ) );
    }

    List var7 = Collections.unmodifiableList( result );
    return var7;
  } finally {
    this.lock.unlock();
  }
}
 
Example #5
Source File: AbstractMemoryDataStore.java    From google-http-java-client with Apache License 2.0 6 votes vote down vote up
@Override
public boolean containsValue(V value) throws IOException {
  if (value == null) {
    return false;
  }
  lock.lock();
  try {
    byte[] serialized = IOUtils.serialize(value);
    for (byte[] bytes : keyValueMap.values()) {
      if (Arrays.equals(serialized, bytes)) {
        return true;
      }
    }
    return false;
  } finally {
    lock.unlock();
  }
}
 
Example #6
Source File: FileDataStoreFactory.java    From google-http-java-client with Apache License 2.0 6 votes vote down vote up
/** @param dataDirectory data directory */
public FileDataStoreFactory(File dataDirectory) throws IOException {
  dataDirectory = dataDirectory.getCanonicalFile();
  // error if it is a symbolic link
  if (IOUtils.isSymbolicLink(dataDirectory)) {
    throw new IOException("unable to use a symbolic link: " + dataDirectory);
  }
  // create parent directory (if necessary)
  if (!dataDirectory.exists() && !dataDirectory.mkdirs()) {
    throw new IOException("unable to create directory: " + dataDirectory);
  }
  this.dataDirectory = dataDirectory;

  if (IS_WINDOWS) {
    setPermissionsToOwnerOnlyWindows(dataDirectory);
  } else {
    setPermissionsToOwnerOnly(dataDirectory);
  }
}
 
Example #7
Source File: FileDataStoreFactory.java    From google-http-java-client with Apache License 2.0 6 votes vote down vote up
FileDataStore(FileDataStoreFactory dataStore, File dataDirectory, String id)
    throws IOException {
  super(dataStore, id);
  this.dataFile = new File(dataDirectory, id);
  // error if it is a symbolic link
  if (IOUtils.isSymbolicLink(dataFile)) {
    throw new IOException("unable to use a symbolic link: " + dataFile);
  }
  // create new file (if necessary)
  if (dataFile.createNewFile()) {
    keyValueMap = Maps.newHashMap();
    // save the credentials to create a new file
    save();
  } else {
    // load credentials from existing file
    keyValueMap = IOUtils.deserialize(new FileInputStream(dataFile));
  }
}
 
Example #8
Source File: MockLowLevelHttpRequest.java    From google-http-java-client with Apache License 2.0 6 votes vote down vote up
/**
 * Returns HTTP content as a string, taking care of any encodings of the content if necessary.
 *
 * <p>Returns an empty string if there is no HTTP content.
 *
 * @since 1.12
 */
public String getContentAsString() throws IOException {
  if (getStreamingContent() == null) {
    return "";
  }
  // write content to a byte[]
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  getStreamingContent().writeTo(out);
  // determine gzip encoding
  String contentEncoding = getContentEncoding();
  if (contentEncoding != null && contentEncoding.contains("gzip")) {
    InputStream contentInputStream =
        new GZIPInputStream(new ByteArrayInputStream(out.toByteArray()));
    out = new ByteArrayOutputStream();
    IOUtils.copy(contentInputStream, out);
  }
  // determine charset parameter from content type
  String contentType = getContentType();
  HttpMediaType mediaType = contentType != null ? new HttpMediaType(contentType) : null;
  Charset charset =
      mediaType == null || mediaType.getCharsetParameter() == null
          ? Charsets.ISO_8859_1
          : mediaType.getCharsetParameter();
  return out.toString(charset.name());
}
 
Example #9
Source File: AppEngineDataStoreFactory.java    From google-http-java-client with Apache License 2.0 6 votes vote down vote up
@Override
public AppEngineDataStore<V> set(String key, V value) throws IOException {
  Preconditions.checkNotNull(key);
  Preconditions.checkNotNull(value);
  lock.lock();
  try {
    Entity entity = new Entity(getId(), key);
    entity.setUnindexedProperty(FIELD_VALUE, new Blob(IOUtils.serialize(value)));
    dataStoreService.put(entity);
    if (memcache != null) {
      memcache.put(key, value, memcacheExpiration);
    }
  } finally {
    lock.unlock();
  }
  return this;
}
 
Example #10
Source File: FileDataStoreFactory.java    From google-http-java-client with Apache License 2.0 6 votes vote down vote up
FileDataStore(FileDataStoreFactory dataStore, File dataDirectory, String id)
    throws IOException {
  super(dataStore, id);
  this.dataFile = new File(dataDirectory, id);
  // error if it is a symbolic link
  if (IOUtils.isSymbolicLink(dataFile)) {
    throw new IOException("unable to use a symbolic link: " + dataFile);
  }
  // create new file (if necessary)
  if (dataFile.createNewFile()) {
    keyValueMap = Maps.newHashMap();
    // save the credentials to create a new file
    save();
  } else {
    // load credentials from existing file
    keyValueMap = IOUtils.deserialize(new FileInputStream(dataFile));
  }
}
 
Example #11
Source File: ByteArrayContentTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
public void subtestConstructor(ByteArrayContent content, String expected) throws IOException {
  assertEquals("type", content.getType());
  assertTrue(content.retrySupported());
  assertEquals(expected.length(), content.getLength());
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  IOUtils.copy(content.getInputStream(), out);
  assertEquals(expected, out.toString());
}
 
Example #12
Source File: AbstractMemoryDataStore.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
public final Collection<V> values() throws IOException {
  lock.lock();
  try {
    List<V> result = Lists.newArrayList();
    for (byte[] bytes : keyValueMap.values()) {
      result.add(IOUtils.<V>deserialize(bytes));
    }
    return Collections.unmodifiableList(result);
  } finally {
    lock.unlock();
  }
}
 
Example #13
Source File: AbstractMemoryDataStore.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
public final V get(String key) throws IOException {
  if (key == null) {
    return null;
  }
  lock.lock();
  try {
    return IOUtils.deserialize(keyValueMap.get(key));
  } finally {
    lock.unlock();
  }
}
 
Example #14
Source File: AbstractMemoryDataStore.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
public final DataStore<V> set(String key, V value) throws IOException {
  Preconditions.checkNotNull(key);
  Preconditions.checkNotNull(value);
  lock.lock();
  try {
    keyValueMap.put(key, IOUtils.serialize(value));
    save();
  } finally {
    lock.unlock();
  }
  return this;
}
 
Example #15
Source File: CustomDataStoreFactory.java    From hop with Apache License 2.0 5 votes vote down vote up
CustomDataStore( CustomDataStoreFactory dataStore, File dataDirectory, String id ) throws IOException {
  super( dataStore, id );
  this.dataDirectory = dataDirectory;
  this.dataFile = new File( this.dataDirectory, getId() );

  if ( IOUtils.isSymbolicLink( this.dataFile ) ) {
    throw new IOException( "unable to use a symbolic link: " + this.dataFile );
  }

  this.keyValueMap = Maps.newHashMap();

  if ( this.dataFile.exists() ) {
    this.keyValueMap = IOUtils.deserialize( new FileInputStream( this.dataFile ) );
  }
}
 
Example #16
Source File: BuyerServiceHelper.java    From googleads-adxbuyer-examples with Apache License 2.0 5 votes vote down vote up
/**
 * reads results from the response object and converts them to a String
 * @param response you want to read content from
 * @return the response content as a String
 * @throws IOException
 */
protected static String getResultAsString(HttpResponse response) throws IOException {
  InputStream content = response.getContent();
  if (content == null) {
    return "";
  }
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  IOUtils.copy(content, out);
  String json = out.toString(response.getContentCharset().name());
  return json;
}
 
Example #17
Source File: HttpResponseUtils.java    From android-oauth-client with Apache License 2.0 5 votes vote down vote up
static String parseAsStringWithoutClosing(HttpResponse response) throws IOException {
    InputStream content = response.getContent();
    if (content == null) {
        return "";
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    IOUtils.copy(content, out, false);
    return out.toString(response.getContentCharset().name());
}
 
Example #18
Source File: CustomDataStoreFactory.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
public CustomDataStoreFactory( File dataDirectory ) throws IOException {
  dataDirectory = dataDirectory.getCanonicalFile();
  this.dataDirectory = dataDirectory;
  if ( IOUtils.isSymbolicLink( dataDirectory ) ) {
    throw new IOException( "unable to use a symbolic link: " + dataDirectory );
  } else if ( !dataDirectory.exists() && !dataDirectory.mkdirs() ) {
    throw new IOException( "unable to create directory: " + dataDirectory );
  }
}
 
Example #19
Source File: CustomDataStoreFactory.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
CustomDataStore( CustomDataStoreFactory dataStore, File dataDirectory, String id ) throws IOException {
  super( dataStore, id );
  this.dataDirectory = dataDirectory;
  this.dataFile = new File( this.dataDirectory, getId() );

  if ( IOUtils.isSymbolicLink( this.dataFile ) ) {
    throw new IOException( "unable to use a symbolic link: " + this.dataFile );
  }

  this.keyValueMap = Maps.newHashMap();

  if ( this.dataFile.exists() ) {
    this.keyValueMap = (HashMap) IOUtils.deserialize( new FileInputStream( this.dataFile ) );
  }
}
 
Example #20
Source File: CustomDataStoreFactory.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
public final DataStore<V> set( String key, V value ) throws IOException {
  Preconditions.checkNotNull( key );
  Preconditions.checkNotNull( value );
  this.lock.lock();

  try {
    this.keyValueMap.put( key, IOUtils.serialize( value ) );
    this.save();
  } finally {
    this.lock.unlock();
  }

  return this;
}
 
Example #21
Source File: CustomDataStoreFactory.java    From hop with Apache License 2.0 5 votes vote down vote up
public CustomDataStoreFactory( File dataDirectory ) throws IOException {
  dataDirectory = dataDirectory.getCanonicalFile();
  this.dataDirectory = dataDirectory;
  if ( IOUtils.isSymbolicLink( dataDirectory ) ) {
    throw new IOException( "unable to use a symbolic link: " + dataDirectory );
  } else if ( !dataDirectory.exists() && !dataDirectory.mkdirs() ) {
    throw new IOException( "unable to create directory: " + dataDirectory );
  }
}
 
Example #22
Source File: CustomDataStoreFactory.java    From hop with Apache License 2.0 5 votes vote down vote up
public final DataStore<V> set( String key, V value ) throws IOException {
  Preconditions.checkNotNull( key );
  Preconditions.checkNotNull( value );
  this.lock.lock();

  try {
    this.keyValueMap.put( key, IOUtils.serialize( value ) );
    this.save();
  } finally {
    this.lock.unlock();
  }

  return this;
}
 
Example #23
Source File: FileDataStoreFactory.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
/** @param dataDirectory data directory */
public FileDataStoreFactory(File dataDirectory) throws IOException {
  dataDirectory = dataDirectory.getCanonicalFile();
  // error if it is a symbolic link
  if (IOUtils.isSymbolicLink(dataDirectory)) {
    throw new IOException("unable to use a symbolic link: " + dataDirectory);
  }
  // create parent directory (if necessary)
  if (!dataDirectory.exists() && !dataDirectory.mkdirs()) {
    throw new IOException("unable to create directory: " + dataDirectory);
  }
  this.dataDirectory = dataDirectory;
  setPermissionsToOwnerOnly(dataDirectory);
}
 
Example #24
Source File: MastodonHttpUtilities.java    From data-transfer-project with Apache License 2.0 5 votes vote down vote up
private String requestRaw(String path) throws IOException {
  HttpRequest getRequest = TRANSPORT.createRequestFactory().buildGetRequest(
      new GenericUrl(baseUrl + path));
  HttpHeaders headers = new HttpHeaders();
  headers.setAuthorization("Bearer " + accessToken);
  getRequest.setHeaders(headers);

  HttpResponse response = getRequest.execute();

  validateResponse(getRequest, response, 200);
  ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

  IOUtils.copy(response.getContent(), byteArrayOutputStream, true);
  return byteArrayOutputStream.toString();
}
 
Example #25
Source File: ArtifactStoreTest.java    From modeldb with Apache License 2.0 4 votes vote down vote up
@Test
public void getArtifactFromCloudTest() {
  LOGGER.info("get artifact from cloud test start................................");

  try {
    ArtifactStoreBlockingStub artifactStoreBlockingStub =
        ArtifactStoreGrpc.newBlockingStub(channel);

    StoreArtifact storeArtifact =
        StoreArtifact.newBuilder()
            .setKey("verta_logo.png")
            .setPath(
                "https://www.verta.ai/static/logo-landing-424af27a5fc184c64225f604232db39e.png")
            .build();

    StoreArtifact.Response response = artifactStoreBlockingStub.storeArtifact(storeArtifact);

    String cloudFileKey = response.getArtifactStoreKey();
    String cloudFilePath = response.getArtifactStorePath();
    LOGGER.log(
        Level.INFO,
        "StoreArtifact.Response : \n cloudFileKey - "
            + cloudFileKey
            + " \n cloudFilePath - "
            + cloudFilePath);

    assumeTrue(cloudFileKey != null && !cloudFileKey.isEmpty());
    assumeTrue(cloudFilePath != null && !cloudFilePath.isEmpty());

    GetArtifact getArtifactRequest = GetArtifact.newBuilder().setKey(cloudFileKey).build();
    GetArtifact.Response getArtifactResponse =
        artifactStoreBlockingStub.getArtifact(getArtifactRequest);
    ByteString responseByteString = getArtifactResponse.getContents();
    InputStream inputStream = responseByteString.newInput();

    String rootPath = System.getProperty("user.dir");
    FileOutputStream fileOutputStream =
        new FileOutputStream(new File(rootPath + File.separator + cloudFileKey));
    IOUtils.copy(inputStream, fileOutputStream);
    fileOutputStream.close();
    inputStream.close();

    File downloadedFile = new File(rootPath + File.separator + cloudFileKey);
    if (!downloadedFile.exists()) {
      fail("File not fount at download destination");
    }
    downloadedFile.delete();

    DeleteArtifact deleteArtifact = DeleteArtifact.newBuilder().setKey(cloudFileKey).build();
    DeleteArtifact.Response deleteArtifactResponse =
        artifactStoreBlockingStub.deleteArtifact(deleteArtifact);
    assertTrue(deleteArtifactResponse.getStatus());

  } catch (Exception e) {
    e.printStackTrace();
    Status status = Status.fromThrowable(e);
    LOGGER.warning(
        "Error Code : " + status.getCode() + " Description : " + status.getDescription());
    fail();
  }

  LOGGER.info("get artifact from cloud test stop................................");
}
 
Example #26
Source File: CustomDataStoreFactory.java    From hop with Apache License 2.0 4 votes vote down vote up
void save() throws IOException {
  this.dataFile.createNewFile();
  IOUtils.serialize( this.keyValueMap, new FileOutputStream( this.dataFile ) );
}
 
Example #27
Source File: CustomDataStoreFactory.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
void save() throws IOException {
  this.dataFile.createNewFile();
  IOUtils.serialize( this.keyValueMap, new FileOutputStream( this.dataFile ) );
}
 
Example #28
Source File: DicomStreamUtilTest.java    From healthcare-dicom-dicomweb-adapter with Apache License 2.0 4 votes vote down vote up
@Override
public void copyTo(OutputStream out) throws IOException {
  IOUtils.copy(this.in, out);
}
 
Example #29
Source File: NFSService.java    From modeldb with Apache License 2.0 4 votes vote down vote up
/**
 * Upload multipart file into respected artifact path
 *
 * @param artifactPath : artifact path
 * @param uploadedFileInputStream : uploaded file input stream
 * @return {@link String} : upload filename
 * @throws ModelDBException ModelDBException
 */
String storeFile(String artifactPath, InputStream uploadedFileInputStream)
    throws ModelDBException {
  LOGGER.trace("NFSService - storeFile called");

  try {
    String cleanArtifactPath = StringUtils.cleanPath(Objects.requireNonNull(artifactPath));
    String[] folders = cleanArtifactPath.split("/");

    StringBuilder folderPath = new StringBuilder();
    for (int i = 0; i < folders.length - 1; i++) {
      folderPath.append(folders[i]);
      folderPath.append(File.separator);
    }
    LOGGER.trace("NFSService - storeFile - folder path : {}", folderPath.toString());

    // Copy file to the target location (Replacing existing file with the same name)
    File foldersExists =
        new File(this.fileStorageLocation + File.separator + folderPath.toString());
    if (!foldersExists.exists()) {
      boolean folderCreatingStatus = foldersExists.mkdirs();
      LOGGER.trace(
          "NFSService - storeFile - folders created : {}, Path: {}",
          folderCreatingStatus,
          foldersExists.getAbsolutePath());
    }
    LOGGER.trace("NFSService - storeFile -  folders found : {}", foldersExists.getAbsolutePath());

    File destinationFile =
        new File(this.fileStorageLocation + File.separator + cleanArtifactPath);
    if (!destinationFile.exists()) {
      boolean destFileCreatingStatus = destinationFile.createNewFile();
      LOGGER.trace(
          "NFSService - storeFile - file created : {}, Path: {}",
          destFileCreatingStatus,
          destinationFile.getAbsolutePath());
    }
    LOGGER.trace("NFSService - storeFile -  file found : {}", foldersExists.getAbsolutePath());
    FileOutputStream fileOutputStream = new FileOutputStream(destinationFile);
    IOUtils.copy(uploadedFileInputStream, fileOutputStream);
    fileOutputStream.close();
    uploadedFileInputStream.close();
    LOGGER.trace(
        "NFSService - storeFile - file stored successfully, target location : {}",
        destinationFile.getAbsolutePath());
    LOGGER.trace("NFSService - storeFile returned");
    return destinationFile.getName();
  } catch (IOException ex) {
    String errorMessage = "Could not store file. Please try again!";
    LOGGER.warn(errorMessage, ex);
    throw new ModelDBException(errorMessage, ex);
  }
}
 
Example #30
Source File: NFSArtifactStoreTest.java    From modeldb with Apache License 2.0 4 votes vote down vote up
@Test
public void getArtifactTest() {
  LOGGER.info("get artifact test start................................");

  try {
    storeArtifactTest();

    GetUrlForArtifact getUrlForArtifactRequest =
        GetUrlForArtifact.newBuilder()
            .setId(experimentRun.getId())
            .setKey(artifactKey)
            .setMethod("GET")
            .setArtifactType(ArtifactType.IMAGE)
            .build();
    GetUrlForArtifact.Response getUrlForArtifactResponse =
        experimentRunServiceStub.getUrlForArtifact(getUrlForArtifactRequest);

    URL url = new URL(getUrlForArtifactResponse.getUrl());
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    InputStream inputStream = connection.getInputStream();

    String rootPath = System.getProperty("user.dir");
    FileOutputStream fileOutputStream =
        new FileOutputStream(new File(rootPath + File.separator + artifactKey));
    IOUtils.copy(inputStream, fileOutputStream);
    fileOutputStream.close();
    inputStream.close();

    File downloadedFile = new File(rootPath + File.separator + artifactKey);
    if (!downloadedFile.exists()) {
      fail("File not fount at download destination");
    }
    downloadedFile.delete();

  } catch (Exception e) {
    e.printStackTrace();
    Status status = Status.fromThrowable(e);
    LOGGER.error(
        "Error Code : " + status.getCode() + " Description : " + status.getDescription());
    fail();
  }

  LOGGER.info("get artifact test stop................................");
}