Commit 3e341528 authored by Greg Messner's avatar Greg Messner
Browse files

Initial check-in.

parent 0de19add
package com.messners.gitlab.api;
import javax.ws.rs.core.Form;
/**
* This class extends the standard JAX-RS Form class to make it fluent.
*/
public class GitLabApiForm extends Form {
/**
* Fluent method for adding query and form parameters to a get() or post() call.
*
* @param formData the Form containing the name/value pairs
* @param name the name of the field/attribute to add
* @param value the value of the field/attribute to add
* @return this GitLabAPiForm instance
*/
protected GitLabApiForm withParam(String name, Object value) throws IllegalArgumentException {
return (withParam(name, value, false));
}
/**
* Fluent method for adding query and form parameters to a get() or post() call.
* If required is true and value is null, will throw an IllegalArgumentException.
*
* @param formData the Form containing the name/value pairs
* @param name the name of the field/attribute to add
* @param value the value of the field/attribute to add
* @param required the field is required flag
* @return this GitLabAPiForm instance
* @throws IllegalArgumentException if a required parameter is null or empty
*/
protected GitLabApiForm withParam(String name, Object value, boolean required) throws IllegalArgumentException {
if (value == null) {
if (required) {
throw new IllegalArgumentException(name + " cannot be empty or null");
}
return (this);
}
String stringValue = value.toString().trim();
if (required && stringValue.length() == 0) {
throw new IllegalArgumentException(name + " cannot be empty or null");
}
this.param(name, stringValue);
return (this);
}
}
package com.messners.gitlab.api;
import java.io.File;
import java.io.IOException;
import javax.ws.rs.core.Response;
public class Utils {
/**
* Creates a File that is unique in the specified directory. If the specified
* filename exists in the directory, "-#" will be appended to the filename until
* a unique filename can be created.
*
* @param directory
* @param filename
* @return a File that is unique in the specified directory
* @throws IOException
*/
public static File createUniqueFile(File directory, String filename) throws IOException {
File uniqueFile = new File(directory, filename);
if (uniqueFile.createNewFile()) {
return (uniqueFile);
}
String extension = "";
int dotIndex = filename.lastIndexOf('.');
if (dotIndex >= 0) {
extension = filename.substring(dotIndex);
filename = filename.substring(0, dotIndex);
}
int fileNumber = 0;
while (!uniqueFile.createNewFile()) {
fileNumber++;
String numberedFilename = String.format("%s-%d%s", filename, fileNumber, extension);
uniqueFile = new File(directory, numberedFilename);
}
return (uniqueFile);
}
/**
* Get the filename from the "Content-Disposition" header of a JAX-RS response.
*
* @param response the JAX-RS Response instance to get the "Content-Disposition" header filename from
* @return the filename from the "Content-Disposition" header of a JAX-RS response, or null
* if the "Content-Disposition" header is not present in the response
*/
public static String getFilenameFromContentDisposition(Response response) {
String disposition = response.getHeaderString("Content-Disposition");
if (disposition == null || disposition.trim().length() == 0)
return (null);
return (disposition.replaceFirst("(?i)^.*filename=\"([^\"]+)\".*$", "$1"));
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment