All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mysql.jdbc.plus.MySqlUrl Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mysql.jdbc.plus;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

/**
 * MySQL URL
 */
@NoArgsConstructor(access = AccessLevel.PROTECTED) // @checkstyle:ok
public class MySqlUrl {
	
	/**
	 * Prefix
	 */
	public static final String PREFIX = "jdbc:mysql:";
	
	/**
	 * URL pattern
	 */
	protected static final String URL_PATTERN = "^" + PREFIX + "([^\\?]+)(.*?)$";
	
	/**
	 * Query starter
	 */
	protected static final String QUERY_STARTER = "?";
	
	/**
	 * Query joiner
	 */
	protected static final String QUERY_JOINER = "&";
	
	/**
	 * Key-value joiner
	 */
	protected static final String KEY_VALUE_JOINER = "=";
	
	/**
	 * URL is valid?
	 * 
	 * @param url URL
	 * @return {@code true} if URL is valid
	 */
	public static boolean isValid(String url) {
		
		return Pattern.compile(URL_PATTERN).matcher(url).matches();
	}
	
	/**
	 * Get {@link Builder}
	 * 
	 * @param url URL
	 * @return {@link Builder}
	 */
	public static Builder builder(String url) {
		
		// check format
		Matcher matcher = Pattern.compile(URL_PATTERN).matcher(url);
		
		if (!matcher.matches()) {
			
			throw new IllegalArgumentException("Failed to parse URL: " + url);
		}
		
		// extract parameters
		Builder builder = new Builder(matcher.group(1));
		
		for (String query : matcher.group(2).replaceAll(Pattern.quote(QUERY_STARTER), "").split(QUERY_JOINER)) {
			
			query = query.trim();
			
			if (query.isEmpty()) {
				
				continue;
			}
			
			String[] pieces = query.split(KEY_VALUE_JOINER);
			
			if (pieces.length != 2) {
				
				continue;
			}
			
			builder.param(pieces[0].trim(), pieces[1].trim());
		}
		
		return builder;
	}
	
	/**
	 * Builder
	 */
	public static class Builder {
		
		/**
		 * Path
		 */
		private String path;
		
		/**
		 * Constructor
		 * 
		 * @param path path
		 */
		protected Builder(String path) {
			
			this.path = path;
		}
		
		/**
		 * Parameters
		 */
		private Map params = new LinkedHashMap<>();
		
		/**
		 * Add parameter
		 * 
		 * @param key key
		 * @param value value
		 * @return {@link Builder}
		 */
		public Builder param(String key, Object value) {
			
			this.params.put(key, value);
			
			return this;
		}
		
		/**
		 * connectTimeout
		 * 
		 * @param value value
		 * @return {@link Builder}
		 */
		public Builder connectTimeout(int value) {
			
			this.params.put("connectTimeout", value);
			
			return this;
		}
		
		/**
		 * socketTimeout
		 * 
		 * @param value value
		 * @return {@link Builder}
		 */
		public Builder socketTimeout(int value) {
			
			this.params.put("socketTimeout", value);
			
			return this;
		}
		
		/**
		 * useUnicode
		 * 
		 * @param value value
		 * @return {@link Builder}
		 */
		public Builder useUnicode(boolean value) {
			
			this.params.put("useUnicode", value);
			
			return this;
		}
		
		/**
		 * characterEncoding
		 * 
		 * @param value value
		 * @return {@link Builder}
		 */
		public Builder characterEncoding(String value) {
			
			this.params.put("characterEncoding", value);
			
			return this;
		}
		
		/**
		 * zeroDateTimeBehavior
		 * 
		 * @param value value
		 * @return {@link Builder}
		 */
		public Builder zeroDateTimeBehavior(String value) {
			
			this.params.put("zeroDateTimeBehavior", value);
			
			return this;
		}
		
		/**
		 * Build URL
		 * 
		 * @return URL
		 */
		public String build() {
			
			List queries = new ArrayList<>();
			
			for (Entry entry : this.params.entrySet()) {
				
				StringBuilder query = new StringBuilder();
				query.append(entry.getKey());
				query.append(KEY_VALUE_JOINER);
				query.append(entry.getValue());
				
				queries.add(query.toString());
			}
			
			StringBuilder url = new StringBuilder();
			url.append(PREFIX);
			url.append(this.path);
			
			if (!queries.isEmpty()) {
				
				url.append(QUERY_STARTER);
				url.append(String.join(QUERY_JOINER, queries));
			}
			
			return url.toString();
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy