Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

F/parse resume #399

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion dataloader.iml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: com.bullhorn:sdk-rest:1.4.46" level="project" />
<orderEntry type="library" name="Maven: com.bullhorn:sdk-rest:1.4.50" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-core:4.2.6.RELEASE" level="project" />
<orderEntry type="library" name="Maven: commons-logging:commons-logging:1.2" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-web:4.2.6.RELEASE" level="project" />
Expand Down Expand Up @@ -159,5 +159,6 @@
<orderEntry type="library" name="Maven: org.objenesis:objenesis:3.2" level="project" />
<orderEntry type="library" name="Maven: javax.xml.bind:jaxb-api:2.3.1" level="project" />
<orderEntry type="library" name="Maven: javax.activation:javax.activation-api:1.2.0" level="project" />
<orderEntry type="library" name="Maven: javax.activation:activation:1.1.1" level="project" />
</component>
</module>
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<dependency>
<groupId>com.bullhorn</groupId>
<artifactId>sdk-rest</artifactId>
<version>1.4.46</version>
<version>1.4.50</version>
</dependency>

<dependency>
Expand Down Expand Up @@ -69,6 +69,12 @@
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>

<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>

<build>
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/bullhorn/dataloader/enums/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public enum Command {
LOAD("load"),
LOAD_ATTACHMENTS("loadAttachments"),
LOGIN("login"),
PARSE_RESUMES("parseResumes"),
META("meta"),
TEMPLATE("template");

Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/bullhorn/dataloader/enums/ErrorInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public enum ErrorInfo {
+ "3. US Short Date: MM/dd/yyyy\n"
+ "4. UK Short Date: dd/MM/yyyy\n"
+ "For more options, see: https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html"),
CANNOT_PARSE_RESUME(440, "Cannot Parse Resume", "Check that the resume file is valid and can be opened and try re-saving file."),

// 500's - Server Errors (Bullhorn responded that there was an internal error)
INTERNAL_SERVER_ERROR(500, "Internal Server Error",
Expand Down
79 changes: 79 additions & 0 deletions src/main/java/com/bullhorn/dataloader/rest/DataLoaderResume.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.bullhorn.dataloader.rest;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.FileUtils;
import org.springframework.web.multipart.MultipartFile;

public class DataLoaderResume implements MultipartFile {

private String name;
private String contentType;
private byte[] data;

private DataLoaderResume() { }

public static DataLoaderResume getInstance(File file,
String contentType) throws IOException {
return new DataLoaderResume()
.name(file.getName())
.contentType(contentType)
.data(FileUtils.readFileToByteArray(file));
}

private DataLoaderResume name(String name) {
this.name = name;
return this;
}

private DataLoaderResume contentType(String contentType) {
this.contentType = contentType;
return this;
}

private DataLoaderResume data(byte[] data) {
this.data = data;
return this;
}

@Override
public String getName() {
return name;
}

@Override
public String getOriginalFilename() {
return name;
}

@Override
public String getContentType() {
return contentType;
}

@Override
public boolean isEmpty() {
return data == null || data.length < 1;
}

@Override
public long getSize() {
return 0;
}

@Override
public byte[] getBytes() {
return data;
}

@Override
public InputStream getInputStream() {
return null;
}

@Override
public void transferTo(File file) throws IllegalStateException {
}
}
14 changes: 14 additions & 0 deletions src/main/java/com/bullhorn/dataloader/rest/HttpResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.bullhorn.dataloader.rest;

import java.net.HttpURLConnection;

public class HttpResult {
public final boolean success;
public final int status;
public String body;

public HttpResult(int status) {
this.status = status;
success = (this.status == HttpURLConnection.HTTP_OK);
}
}
153 changes: 153 additions & 0 deletions src/main/java/com/bullhorn/dataloader/rest/MultipartUtility.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package com.bullhorn.dataloader.rest;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;

public class MultipartUtility {
private static final Logger log = Logger.getLogger(MultipartUtility.class);

private static final String LINE_FEED = "\r\n";
private static final String CHARSET = "UTF-8";

private final String boundary;
private HttpURLConnection httpConn;
private OutputStream outputStream;
private PrintWriter writer;

/**
* Initializes a new HTTP PUT request with content type set to multipart/form-data
*/
public MultipartUtility(String requestUrl) throws IOException {
boundary = "===" + System.currentTimeMillis() + "===";

URL url = new URL(requestUrl);
httpConn = (HttpURLConnection) url.openConnection();
httpConn.setUseCaches(false);
httpConn.setRequestMethod("PUT");
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
httpConn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);

outputStream = httpConn.getOutputStream();
writer = new PrintWriter(new OutputStreamWriter(outputStream, CHARSET), true);
}

/**
* Adds a form field to the request
*
* @param name field name
* @param value field value
*/
public void addFormField(String name, String value) {
writer.append("--" + boundary).append(LINE_FEED);
writer.append("Content-Disposition: form-data; name=\"" + name + "\"");
writer.append(LINE_FEED);
writer.append("Content-Type: text/plain; charset=" + CHARSET);
writer.append(LINE_FEED);
writer.append(LINE_FEED);
writer.append(value).append(LINE_FEED);
writer.flush();
}

/**
* Adds an upload file section to the request
*
* @param fieldName name attribute in <input type="file" name="..." />
* @param uploadFile a File to be uploaded
*/
public void addFilePart(String fieldName, File uploadFile) throws IOException {
addFilePart(fieldName, uploadFile, uploadFile.getName());
}


/**
* Adds an upload file section to the request
*
* @param fieldName name attribute in <input type="file" name="..." />
* @param uploadFile a File to be uploaded
*/
public void addFilePart(String fieldName, File uploadFile, String fileName) throws IOException {
writer.append("--" + boundary);
writer.append(LINE_FEED);
writer.append("Content-Disposition: form-data; name=\"" + fieldName + "\"; filename=\"" + fileName + "\"");
writer.append(LINE_FEED);
writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(fileName));
writer.append(LINE_FEED);
writer.append("Content-Transfer-Encoding: binary");
writer.append(LINE_FEED);
writer.append(LINE_FEED);
writer.flush();

FileInputStream inputStream = new FileInputStream(uploadFile);
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.flush();
inputStream.close();
writer.append(LINE_FEED);
writer.flush();
}

/**
* Adds a header field to the request.
*
* @param name - name of the header field
* @param value - value of the header field
*/
public void addHeaderField(String name, String value) {
writer.append(name + ": " + value).append(LINE_FEED);
writer.flush();
}

/**
* Completes the request and receives response from the server.
*
* @return a list of Strings as response when server returns OK status, exception otherwise.
*/
public HttpResult finish() throws IOException {
writer.append(LINE_FEED).flush();
writer.append("--" + boundary + "--").append(LINE_FEED);
writer.close();

HttpResult httpResult = readIntoHttpResult(httpConn);
httpConn.disconnect();

return httpResult;
}

public static HttpResult readIntoHttpResult(HttpURLConnection httpConn) throws IOException {
HttpResult httpResult = new HttpResult(httpConn.getResponseCode());
if (httpResult.status < HttpURLConnection.HTTP_BAD_REQUEST) {
httpResult.body = readStream(httpConn.getInputStream());
} else {
httpResult.body = readStream(httpConn.getErrorStream());
}
return httpResult;
}

private static String readStream(InputStream input) {
try {
StringWriter writer = new StringWriter();
IOUtils.copy(input, writer, CHARSET);
return writer.toString();
} catch (Exception ex) {
log.error("Error reading HTML stream", ex);
}

return null;
}
}
50 changes: 50 additions & 0 deletions src/main/java/com/bullhorn/dataloader/rest/Parameters.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.bullhorn.dataloader.rest;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

public class Parameters {
private Map<String, String> params = new HashMap<>();

public void put(String name, Integer value) {
put(name, (null == value) ? null : value.toString());
}

public void put(String name, Boolean value) {
put(name, (null == value) ? null : value.toString());
}

public void put(String name, String value) {
if (StringUtils.isEmpty(name)) {
throw new IllegalArgumentException("'name' can not be blank");
}
params.put(name, value);
}

public String build() {
List<String> paramList = new ArrayList<>(params.size());
for (Map.Entry<String, String> next : params.entrySet()) {
paramList.add(next.getKey() + "=" + encode(next.getValue()));
}

return "?" + StringUtils.join(paramList, "&");
}

private String encode(String value) {
if (value == null) {
return "";
}

try {
return URLEncoder.encode(value, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
}
43 changes: 43 additions & 0 deletions src/main/java/com/bullhorn/dataloader/rest/ParseAsNewResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.bullhorn.dataloader.rest;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
public class ParseAsNewResult {
private Boolean success = false;
private Integer id;
private Boolean duplicate = false;
private String errorMessage;

public Boolean getSuccess() {
return success;
}

public void setSuccess(Boolean success) {
this.success = success;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public Boolean getDuplicate() {
return duplicate;
}

public void setDuplicate(Boolean duplicate) {
this.duplicate = duplicate;
}

public String getErrorMessage() {
return errorMessage;
}

public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
}
Loading