/// <exception cref="System.IO.IOException"/>
		private static void ProcessUrl(Uri url)
		{
			URLConnection con = url.OpenConnection();
			//        con.setConnectTimeout(connectTimeout);
			//        con.setReadTimeout(readTimeout);
			InputStream @in = con.GetInputStream();
			// Read metadata
			Com.Drew.Metadata.Metadata metadata;
			try
			{
				metadata = ImageMetadataReader.ReadMetadata(@in);
			}
			catch (ImageProcessingException e)
			{
				// this is an error in the Jpeg segment structure.  we're looking for bad handling of
				// metadata segments.  in this case, we didn't even get a segment.
				System.Console.Error.Printf("%s: %s [Error Extracting Metadata]\n\t%s%n", e.GetType().FullName, url, e.Message);
				return;
			}
			catch (Exception t)
			{
				// general, uncaught exception during processing of jpeg segments
				System.Console.Error.Printf("%s: %s [Error Extracting Metadata]%n", t.GetType().FullName, url);
				Sharpen.Runtime.PrintStackTrace(t, System.Console.Error);
				return;
			}
			if (metadata.HasErrors())
			{
				System.Console.Error.Println(url);
				foreach (Com.Drew.Metadata.Directory directory in metadata.GetDirectories())
				{
					if (!directory.HasErrors())
					{
						continue;
					}
					foreach (string error in directory.GetErrors())
					{
						System.Console.Error.Printf("\t[%s] %s%n", directory.GetName(), error);
					}
				}
			}
			// Iterate through all values
			foreach (Com.Drew.Metadata.Directory directory_1 in metadata.GetDirectories())
			{
				foreach (Tag tag in directory_1.GetTags())
				{
					string tagName = tag.GetTagName();
					string directoryName = directory_1.GetName();
					string description = tag.GetDescription();
					// truncate the description if it's too long
					if (description != null && description.Length > 1024)
					{
						description = Sharpen.Runtime.Substring(description, 0, 1024) + "...";
					}
					System.Console.Out.Printf("[%s] %s = %s%n", directoryName, tagName, description);
				}
			}
		}
 /// <summary>
 /// Sets the attachment with the given name.
 /// </summary>
 /// <remarks>
 /// Sets the <see cref="Couchbase.Lite.Attachment"/> with the given name. 
 /// The <see cref="Couchbase.Lite.Attachment"/> data will be written to 
 /// the <see cref="Couchbase.Lite.Database"/> when the 
 /// <see cref="Couchbase.Lite.Revision"/> is saved.
 /// </remarks>
 /// <param name="name">The name of the <see cref="Couchbase.Lite.Attachment"/> to set.</param>
 /// <param name="contentType">The content-type of the <see cref="Couchbase.Lite.Attachment"/>.</param>
 /// <param name="contentUrl">The URL of the <see cref="Couchbase.Lite.Attachment"/> content.</param>
 public void SetAttachment(String name, String contentType, Uri contentUrl) {
     try
     {
         var inputStream = contentUrl.OpenConnection().GetInputStream();
         var length = inputStream.Length;
         var inputBytes = inputStream.ReadAllBytes();
         inputStream.Close();
         SetAttachment(name, contentType, inputBytes);
     }
     catch (IOException e)
     {
         Log.E(Database.Tag, "Error opening stream for url: " + contentUrl);
         throw new RuntimeException(e);
     }
 }
        /// <summary>
        /// Sets the attachment with the given name.
        /// </summary>
        /// <remarks>
        /// Sets the <see cref="Couchbase.Lite.Attachment"/> with the given name. 
        /// The <see cref="Couchbase.Lite.Attachment"/> data will be written to 
        /// the <see cref="Couchbase.Lite.Database"/> when the 
        /// <see cref="Couchbase.Lite.Revision"/> is saved.
        /// </remarks>
        /// <param name="name">The name of the <see cref="Couchbase.Lite.Attachment"/> to set.</param>
        /// <param name="contentType">The content-type of the <see cref="Couchbase.Lite.Attachment"/>.</param>
        /// <param name="contentUrl">The URL of the <see cref="Couchbase.Lite.Attachment"/> content.</param>
        public void SetAttachment(String name, String contentType, Uri contentUrl) {
            try {
                byte[] inputBytes = null;
                using(var inputStream = contentUrl.OpenConnection().GetInputStream()) {
                    var length = inputStream.Length;
                    inputBytes = inputStream.ReadAllBytes();
                }

                SetAttachment(name, contentType, inputBytes);
            } catch (IOException e) {
                Log.E(Database.TAG, "Error opening stream for url: {0}", contentUrl);
                throw new Exception(String.Format("Error opening stream for url: {0}", contentUrl), e);
            }
        }
        internal virtual HttpURLConnection SendRequest(string method, string path, 
            IDictionary<string, string> headers, IDictionary<string, object> bodyObj)
        {
            try
            {
                var url = new Uri(new Uri((string)bodyObj["remote_url"]), path);
                var conn = url.OpenConnection();
                conn.SetDoOutput(true);
                conn.SetRequestMethod(method);
                if (headers != null)
                {
                    foreach (string header in headers.Keys)
                    {
                        conn.SetRequestProperty(header, headers[header]);
                    }
                }
                var allProperties = conn.GetRequestProperties();
                if (bodyObj != null)
                {
                    //conn.SetDoInput(true);
                    var bais = mapper.WriteValueAsBytes(bodyObj);
                    conn.SetRequestInputStream(bais);
                }
/*                var router = new Couchbase.Lite.Router.Router(manager, conn);
                router.Start();
*/              return conn;
            }
            catch (UriFormatException)
            {
                Assert.Fail();
            }
            catch (IOException)
            {
                Assert.Fail();
            }
            return null;
        }
Example #5
0
		/// <exception cref="System.IO.IOException"></exception>
		private HttpURLConnection Open(string method, string bucket, string key, IDictionary
			<string, string> args)
		{
			StringBuilder urlstr = new StringBuilder();
			urlstr.Append("http://");
			urlstr.Append(bucket);
			urlstr.Append('.');
			urlstr.Append(DOMAIN);
			urlstr.Append('/');
			if (key.Length > 0)
			{
				HttpSupport.Encode(urlstr, key);
			}
			if (!args.IsEmpty())
			{
				Iterator<KeyValuePair<string, string>> i;
				urlstr.Append('?');
				i = args.EntrySet().Iterator();
				while (i.HasNext())
				{
					KeyValuePair<string, string> e = i.Next();
					urlstr.Append(e.Key);
					urlstr.Append('=');
					HttpSupport.Encode(urlstr, e.Value);
					if (i.HasNext())
					{
						urlstr.Append('&');
					}
				}
			}
			Uri url = new Uri(urlstr.ToString());
			Proxy proxy = HttpSupport.ProxyFor(proxySelector, url);
			HttpURLConnection c;
			c = (HttpURLConnection)url.OpenConnection(proxy);
			c.SetRequestMethod(method);
			c.SetRequestProperty("User-Agent", "jgit/1.0");
			c.SetRequestProperty("Date", HttpNow());
			return c;
		}
Example #6
0
		/// <exception cref="System.IO.IOException"></exception>
		private static string ReadUrl(string filePath, string charCoding, bool urlIsFile)
		{
			int chunkLength;
			Stream @is = null;
			try
			{
				if (!urlIsFile)
				{
					Uri urlObj = new Uri(filePath);
					URLConnection uc = urlObj.OpenConnection();
					@is = uc.GetInputStream();
					chunkLength = uc.GetContentLength();
					if (chunkLength <= 0)
					{
						chunkLength = 1024;
					}
					if (charCoding == null)
					{
						string type = uc.GetContentType();
						if (type != null)
						{
							charCoding = GetCharCodingFromType(type);
						}
					}
				}
				else
				{
					FilePath f = new FilePath(filePath);
					if (!f.Exists())
					{
						throw new FileNotFoundException("File not found: " + filePath);
					}
					else
					{
						if (!f.CanRead())
						{
							throw new IOException("Cannot read file: " + filePath);
						}
					}
					long length = f.Length();
					chunkLength = (int)length;
					if (chunkLength != length)
					{
						throw new IOException("Too big file size: " + length);
					}
					if (chunkLength == 0)
					{
						return string.Empty;
					}
					@is = new FileInputStream(f);
				}
				TextReader r;
				if (charCoding == null)
				{
					r = new StreamReader(@is);
				}
				else
				{
					r = new StreamReader(@is, charCoding);
				}
				return ReadReader(r, chunkLength);
			}
			finally
			{
				if (@is != null)
				{
					@is.Close();
				}
			}
		}
		/// <summary>
		/// Can be overridden in subclasses to customize the URL connection opening
		/// process.
		/// </summary>
		/// <remarks>
		/// Can be overridden in subclasses to customize the URL connection opening
		/// process. By default, just calls
		/// <see cref="System.Uri.OpenConnection()">System.Uri.OpenConnection()</see>
		/// .
		/// </remarks>
		/// <param name="url">the URL</param>
		/// <returns>a connection to the URL.</returns>
		/// <exception cref="System.IO.IOException">if an I/O error occurs.</exception>
		protected internal virtual URLConnection OpenUrlConnection(Uri url)
		{
			return url.OpenConnection();
		}