Skip to content

Commit

Permalink
Merge pull request #95 from metamug/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
d3ep4k authored Sep 10, 2019
2 parents d86adb5 + 12456e1 commit 202e316
Show file tree
Hide file tree
Showing 15 changed files with 882 additions and 362 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.metamug</groupId>
<artifactId>mason</artifactId>
<version>3.0</version>
<version>3.1</version>
<packaging>jar</packaging>
<name>mason</name>
<properties>
Expand Down Expand Up @@ -157,7 +157,7 @@
<dependency>
<groupId>com.metamug</groupId>
<artifactId>mtg-api</artifactId>
<version>1.2.0</version>
<version>1.5</version>
</dependency>
<dependency>
<groupId>javax</groupId>
Expand Down
112 changes: 53 additions & 59 deletions src/main/java/com/metamug/mason/entity/response/DatasetOutput.java
Original file line number Diff line number Diff line change
@@ -1,59 +1,53 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.metamug.mason.entity.response;

import java.util.Map;
import java.util.SortedMap;
import javax.xml.bind.JAXBException;
import org.apache.taglibs.standard.tag.common.sql.ResultImpl;
import org.json.JSONArray;
import org.json.JSONObject;

/**
* Dataset JSON output object
*/
public class DatasetOutput extends JSONOutput {

public static final String COLUMNS = "columns";
public static final String DATASET = "dataset";

public DatasetOutput(Map<String, Object> outputMap) throws JAXBException {
super(outputMap);
}

@Override
protected Object getJson(ResultImpl impl) {
return resultSetToDataSet(impl);
}

private JSONObject resultSetToDataSet(ResultImpl resultImpl) {
SortedMap[] rows = resultImpl.getRows();
String[] columnNames = resultImpl.getColumnNames();
JSONObject object = new JSONObject();
JSONArray columnArray = new JSONArray();
for (int i = 0; i < columnNames.length; i++) {
String columnName = columnNames[i].isEmpty() || columnNames[i].equalsIgnoreCase("null") ? "col" + i : columnNames[i];
columnArray.put(columnName);
}
object.put(COLUMNS, columnArray);
JSONArray dataSetArray = new JSONArray();
for (SortedMap row : rows) {
JSONArray rowArray = new JSONArray();
for (int i = 0; i < columnNames.length; i++) {
String columnName = columnNames[i].isEmpty() || columnNames[i].equalsIgnoreCase("null") ? "col" + i : columnNames[i];
rowArray.put((row.get(columnName) != null) ? row.get(columnName) : "null");
}
dataSetArray.put(rowArray);
}
object.put(DATASET, dataSetArray);
return object;
}

@Override
public String getContentType() {
return HEADER_DATASET;
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.metamug.mason.entity.response;

import java.util.SortedMap;
import org.apache.taglibs.standard.tag.common.sql.ResultImpl;
import org.json.JSONArray;
import org.json.JSONObject;

/**
* Dataset JSON output object
*/
public class DatasetOutput extends JSONOutput {

public static final String COLUMNS = "columns";
public static final String DATASET = "dataset";

@Override
protected Object getJson(ResultImpl impl) {
return resultSetToDataSet(impl);
}

private JSONObject resultSetToDataSet(ResultImpl resultImpl) {
SortedMap[] rows = resultImpl.getRows();
String[] columnNames = resultImpl.getColumnNames();
JSONObject object = new JSONObject();
JSONArray columnArray = new JSONArray();
for (int i = 0; i < columnNames.length; i++) {
String columnName = columnNames[i].isEmpty() || columnNames[i].equalsIgnoreCase("null") ? "col" + i : columnNames[i];
columnArray.put(columnName);
}
object.put(COLUMNS, columnArray);
JSONArray dataSetArray = new JSONArray();
for (SortedMap row : rows) {
JSONArray rowArray = new JSONArray();
for (int i = 0; i < columnNames.length; i++) {
String columnName = columnNames[i].isEmpty() || columnNames[i].equalsIgnoreCase("null") ? "col" + i : columnNames[i];
rowArray.put((row.get(columnName) != null) ? row.get(columnName) : "null");
}
dataSetArray.put(rowArray);
}
object.put(DATASET, dataSetArray);
return object;
}

@Override
public String getContentType() {
return HEADER_DATASET;
}
}
35 changes: 20 additions & 15 deletions src/main/java/com/metamug/mason/entity/response/FileOutput.java
Original file line number Diff line number Diff line change
Expand Up @@ -508,29 +508,23 @@ You should also get your employer (if you work as a programmer) or your
*/
package com.metamug.mason.entity.response;

import com.metamug.entity.Attachment;
import com.metamug.entity.Response;
import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
*
* @author pc
*/
public class FileOutput extends MasonOutput<InputStream> {
public class FileOutput extends MasonOutput<Attachment> {

private InputStream content;
private Attachment content;

public FileOutput(Map<String, Object> outputMap) {
super(outputMap);

outputMap.forEach((key, value) -> {
//Takes the last matched file
if (value instanceof Response) {
content = (InputStream) ((Response) value).getPayload();
}

});
public Map<String, String> getExtraHeaders() {
Map<String, String> headers = new HashMap<String, String>();
headers.put("Content-Disposition", "attachment; filename=\"" + content.getName() + "\"");
return headers;
}

@Override
Expand All @@ -539,10 +533,21 @@ public String getContentType() {
}

@Override
public InputStream getContent() {
public Attachment getContent() {
outputMap.forEach((key, value) -> {
//Takes the last matched file
if (value instanceof Attachment) {
content = (Attachment) value;
}
});
return content;
}

public static final String OCTETSTREAM = "application/octet-stream";

@Override
public String format(Response masonResponse) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

}
40 changes: 28 additions & 12 deletions src/main/java/com/metamug/mason/entity/response/JSONOutput.java
Original file line number Diff line number Diff line change
Expand Up @@ -506,13 +506,17 @@
*/
package com.metamug.mason.entity.response;

import com.metamug.entity.Request;
import com.metamug.entity.Response;
import com.metamug.exec.ResponseFormatter;
import com.metamug.mason.io.mpath.MPathUtil;
import com.metamug.mason.io.objectreturn.ObjectReturn;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.JAXBException;
import javax.xml.bind.MarshalException;
import org.apache.taglibs.standard.tag.common.sql.ResultImpl;
Expand All @@ -522,13 +526,11 @@
/**
* JSONs Output Object
*/
public class JSONOutput extends MasonOutput<String> {

protected JSONObject responseJson = new JSONObject();

public JSONOutput(Map<String, Object> outputMap) throws JAXBException {
super(outputMap);
public class JSONOutput extends MasonOutput<JSONObject>{

@Override
protected JSONObject getContent() {
JSONObject responseJson = new JSONObject();
for (Map.Entry<String, Object> entry : outputMap.entrySet()) {
Object obj = entry.getValue();
String key = entry.getKey();
Expand All @@ -542,7 +544,12 @@ public JSONOutput(Map<String, Object> outputMap) throws JAXBException {
} else if (obj instanceof List) {
JSONArray array = new JSONArray();
for (Object o : (Iterable<? extends Object>) obj) {
array.put(new JSONObject(ObjectReturn.convert(o, HEADER_JSON)));
try {
array.put(new JSONObject(ObjectReturn.convert(o, HEADER_JSON)));
} catch (JAXBException ex) {
//@TODO Do something here
Logger.getLogger(JSONOutput.class.getName()).log(Level.SEVERE, null, ex);
}
}
responseJson.put(key, array);
} else {
Expand All @@ -552,16 +559,25 @@ public JSONOutput(Map<String, Object> outputMap) throws JAXBException {
responseJson.put(key, new JSONObject(ObjectReturn.convert(obj, HEADER_JSON)));
} catch (MarshalException mex) {
responseJson.put(key, obj);
} catch (JAXBException ex) {
Logger.getLogger(JSONOutput.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
return responseJson;
}

@Override
public String format(Response response) {
JSONObject responseJson = (JSONObject) response.getPayload();
return responseJson.toString();
}

protected Object getJson(ResultImpl impl) {
return resultSetToJson(impl);
}

protected JSONArray resultSetToJson(ResultImpl resultImpl) {
private JSONArray resultSetToJson(ResultImpl resultImpl) {
SortedMap[] rows = resultImpl.getRows();
String[] columnNames = resultImpl.getColumnNames();
JSONArray array = new JSONArray();
Expand Down Expand Up @@ -596,8 +612,8 @@ public String getContentType() {
}

@Override
public String getContent() {
return responseJson.toString();
protected Map<String, String> getExtraHeaders() {
return new HashMap<>();
}

}
63 changes: 56 additions & 7 deletions src/main/java/com/metamug/mason/entity/response/MasonOutput.java
Original file line number Diff line number Diff line change
Expand Up @@ -506,24 +506,73 @@
*/
package com.metamug.mason.entity.response;

import java.io.UnsupportedEncodingException;
import com.metamug.entity.Request;
import com.metamug.entity.Response;
import com.metamug.exec.ResponseGenerator;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import com.metamug.exec.ResponseFormatter;

/**
* Generic Output Object
*
* @param <T>
*/
public abstract class MasonOutput<T> {
public abstract class MasonOutput<T> implements ResponseGenerator, ResponseFormatter {

public static final String HEADER_JSON = "application/json";
public static final String HEADER_DATASET = "application/json+dataset";
public static final String HEADER_XML = "application/xml";
Map<String, Object> responseMap;
protected Map<String, Object> outputMap;

public MasonOutput(Map<String, Object> outputMap) {
/**
* If Extra Headers are added later, and Content-Type header is part of it.
* This value from this method will be overwritten in the response header
*
* @return
*/
protected abstract String getContentType();

}
/**
* Protected from being used outside the package. Use Response Object to get
* payload
*
* @return
*/
protected abstract T getContent();

/**
* Add Extra Headers to the response
*
* @return
*
*/
abstract protected Map<String, String> getExtraHeaders();

@Override
public Response generate(Request request, Map<String, Object> outputMap) {

this.outputMap = outputMap;
//Response takes Any Object as its payload
Response finalResponse = new Response(getContent());

public abstract String getContentType();
getExtraHeaders().forEach((k, v) -> finalResponse.setHeader(k, v));

// InputStream targetStream = null;
// if (content instanceof String) {
// targetStream = new ByteArrayInputStream(((String) content).getBytes());
// } else if (content instanceof InputStream) {
// targetStream = (InputStream) content;
// }
if (StringUtils.isEmpty(getContentType())) {
//@TODO Set from the request
} else {
finalResponse.setHeader("Content-Type", getContentType());
}

return finalResponse;

}

public abstract T getContent();
abstract public String format(Response masonResponse);
}
Loading

0 comments on commit 202e316

Please sign in to comment.