Skip to content

Commit

Permalink
code format
Browse files Browse the repository at this point in the history
  • Loading branch information
bancika committed Jan 2, 2011
1 parent 246c368 commit 914ac6c
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
* Title: Client HTTP Request class
* </p>
* <p>
* Description: this class helps to send POST HTTP requests with various form data, including files.
* Cookies can be added to be included in the request.
* Description: this class helps to send POST HTTP requests with various form
* data, including files. Cookies can be added to be included in the request.
* </p>
*
* @author Vlad Patryshev
Expand Down Expand Up @@ -70,7 +70,8 @@ private void boundary() throws IOException {
}

/**
* Creates a new multipart POST HTTP request on a freshly opened URLConnection
* Creates a new multipart POST HTTP request on a freshly opened
* URLConnection
*
* @param connection
* an already open URL connection
Expand Down Expand Up @@ -150,8 +151,8 @@ public void setCookies(Map cookies) throws IOException {
* adds cookies to the request
*
* @param cookies
* array of cookie names and values (cookies[2*i] is a name, cookies[2*i + 1] is a
* value)
* array of cookie names and values (cookies[2*i] is a name,
* cookies[2*i + 1] is a value)
* @throws IOException
*/
public void setCookies(String[] cookies) throws IOException {
Expand Down Expand Up @@ -243,13 +244,15 @@ public void setParameter(String name, File file) throws IOException {
}

/**
* adds a parameter to the request; if the parameter is a File, the file is uploaded, otherwise
* the string value of the parameter is passed in the request
* adds a parameter to the request; if the parameter is a File, the file is
* uploaded, otherwise the string value of the parameter is passed in the
* request
*
* @param name
* parameter name
* @param object
* parameter value, a File or anything else that can be stringified
* parameter value, a File or anything else that can be
* stringified
* @throws IOException
*/
public void setParameter(String name, Object object) throws IOException {
Expand All @@ -264,8 +267,9 @@ public void setParameter(String name, Object object) throws IOException {
* adds parameters to the request
*
* @param parameters
* "name-to-value" map of parameters; if a value is a file, the file is uploaded,
* otherwise it is stringified and sent in the request
* "name-to-value" map of parameters; if a value is a file, the
* file is uploaded, otherwise it is stringified and sent in the
* request
* @throws IOException
*/
public void setParameters(Map parameters) throws IOException {
Expand All @@ -281,9 +285,10 @@ public void setParameters(Map parameters) throws IOException {
* adds parameters to the request
*
* @param parameters
* array of parameter names and values (parameters[2*i] is a name, parameters[2*i +
* 1] is a value); if a value is a file, the file is uploaded, otherwise it is
* stringified and sent in the request
* array of parameter names and values (parameters[2*i] is a
* name, parameters[2*i + 1] is a value); if a value is a file,
* the file is uploaded, otherwise it is stringified and sent in
* the request
* @throws IOException
*/
public void setParameters(Object[] parameters) throws IOException {
Expand All @@ -295,7 +300,8 @@ public void setParameters(Object[] parameters) throws IOException {
}

/**
* posts the requests to the server, with all the cookies and parameters that were added
* posts the requests to the server, with all the cookies and parameters
* that were added
*
* @return input stream with the server response
* @throws IOException
Expand All @@ -308,8 +314,9 @@ public InputStream post() throws IOException {
}

/**
* posts the requests to the server, with all the cookies and parameters that were added before
* (if any), and with parameters that are passed in the argument
* posts the requests to the server, with all the cookies and parameters
* that were added before (if any), and with parameters that are passed in
* the argument
*
* @param parameters
* request parameters
Expand All @@ -323,8 +330,9 @@ public InputStream post(Map parameters) throws IOException {
}

/**
* posts the requests to the server, with all the cookies and parameters that were added before
* (if any), and with parameters that are passed in the argument
* posts the requests to the server, with all the cookies and parameters
* that were added before (if any), and with parameters that are passed in
* the argument
*
* @param parameters
* request parameters
Expand All @@ -338,8 +346,9 @@ public InputStream post(Object[] parameters) throws IOException {
}

/**
* posts the requests to the server, with all the cookies and parameters that were added before
* (if any), and with cookies and parameters that are passed in the arguments
* posts the requests to the server, with all the cookies and parameters
* that were added before (if any), and with cookies and parameters that are
* passed in the arguments
*
* @param cookies
* request cookies
Expand All @@ -357,8 +366,9 @@ public InputStream post(Map cookies, Map parameters) throws IOException {
}

/**
* posts the requests to the server, with all the cookies and parameters that were added before
* (if any), and with cookies and parameters that are passed in the arguments
* posts the requests to the server, with all the cookies and parameters
* that were added before (if any), and with cookies and parameters that are
* passed in the arguments
*
* @param cookies
* request cookies
Expand Down Expand Up @@ -467,7 +477,8 @@ public InputStream post(String name1, Object value1, String name2, Object value2
}

/**
* posts a new request to specified URL, with parameters that are passed in the argument
* posts a new request to specified URL, with parameters that are passed in
* the argument
*
* @param parameters
* request parameters
Expand All @@ -480,7 +491,8 @@ public static InputStream post(URL url, Map parameters) throws IOException {
}

/**
* posts a new request to specified URL, with parameters that are passed in the argument
* posts a new request to specified URL, with parameters that are passed in
* the argument
*
* @param parameters
* request parameters
Expand All @@ -493,8 +505,8 @@ public static InputStream post(URL url, Object[] parameters) throws IOException
}

/**
* posts a new request to specified URL, with cookies and parameters that are passed in the
* argument
* posts a new request to specified URL, with cookies and parameters that
* are passed in the argument
*
* @param cookies
* request cookies
Expand All @@ -510,8 +522,8 @@ public static InputStream post(URL url, Map cookies, Map parameters) throws IOEx
}

/**
* posts a new request to specified URL, with cookies and parameters that are passed in the
* argument
* posts a new request to specified URL, with cookies and parameters that
* are passed in the argument
*
* @param cookies
* request cookies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ public interface IFlatProxy {
* parameters.
* @return streamed server response
*/
public InputStream invoke(String url, String methodName,
Map<String, Object> params);
public InputStream invoke(String url, String methodName, Map<String, Object> params);

/**
* Invokes the specified method on the specified url and returns
Expand All @@ -41,6 +40,5 @@ public InputStream invoke(String url, String methodName,
* parameters.
* @return deserialized server response
*/
public Object invokeAndDeserialize(String url, String methodName,
Map<String, Object> params);
public Object invokeAndDeserialize(String url, String methodName, Map<String, Object> params);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ public class JavaStringSource extends SimpleJavaFileObject {
final String code;

JavaStringSource(String name, String code) {
super(URI.create("string:///" + name.replace('.', '/')
+ Kind.SOURCE.extension), Kind.SOURCE);
super(URI.create("string:///" + name.replace('.', '/') + Kind.SOURCE.extension),
Kind.SOURCE);
this.code = code;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ public class PhpFlatProxy implements IFlatProxy {
private static final Logger LOG = Logger.getLogger(PhpFlatProxy.class);

@Override
public InputStream invoke(String url, String methodName,
Map<String, Object> params) {
public InputStream invoke(String url, String methodName, Map<String, Object> params) {
InputStream serverInput;
try {
// Flatten params map into an array
Expand All @@ -55,8 +54,7 @@ public InputStream invoke(String url, String methodName,
}

@Override
public Object invokeAndDeserialize(String url, String methodName,
Map<String, Object> params) {
public Object invokeAndDeserialize(String url, String methodName, Map<String, Object> params) {
InputStream stream = invoke(url, methodName, params);
if (stream == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ public ProxyFactory(IFlatProxy flatProxy) {
super();
this.flatProxy = flatProxy;

this.fileClassLoader = new FileClassLoader(ToolProvider
.getSystemToolClassLoader());
this.fileClassLoader = new FileClassLoader(ToolProvider.getSystemToolClassLoader());
this.compiler = ToolProvider.getSystemJavaCompiler();
}

Expand All @@ -62,32 +61,24 @@ public ProxyFactory(IFlatProxy flatProxy) {
@SuppressWarnings("unchecked")
public <T> T createProxy(Class<? extends T> clazz, String url) {
if (!clazz.isInterface()) {
LOG
.error("Specified class is not an interface: "
+ clazz.getName());
LOG.error("Specified class is not an interface: " + clazz.getName());
return null;
}
String interfaceName = clazz.getName();
String newClassName = interfaceName.substring(interfaceName
.lastIndexOf(".") + 1)
+ "Impl";
String newClassName = interfaceName.substring(interfaceName.lastIndexOf(".") + 1) + "Impl";
try {
LOG.info("Creating temp class code");
StringWriter writer = new StringWriter();
PrintWriter out = new PrintWriter(writer);

out.println("public class " + newClassName + " implements "
+ interfaceName + " {\n");
out
.println(" private " + IFlatProxy.class.getName()
+ " proxy;\n");
out.println("public class " + newClassName + " implements " + interfaceName + " {\n");
out.println(" private " + IFlatProxy.class.getName() + " proxy;\n");
// Generate a constructor
out.println(" public " + newClassName + "("
+ IFlatProxy.class.getName()
out.println(" public " + newClassName + "(" + IFlatProxy.class.getName()
+ " proxy) { this.proxy = proxy; }\n");
for (Method method : clazz.getDeclaredMethods()) {
out.print(" public " + method.getReturnType().getName() + " "
+ method.getName() + "(");
out.print(" public " + method.getReturnType().getName() + " " + method.getName()
+ "(");
for (int i = 0; i < method.getParameterTypes().length; i++) {
if (i > 0) {
out.print(", ");
Expand All @@ -102,8 +93,7 @@ public <T> T createProxy(Class<? extends T> clazz, String url) {
.println(" java.util.Map<String, Object> params = new java.util.HashMap<String, Object>();");
for (int i = 0; i < method.getParameterTypes().length; i++) {
String paramName = extractParameterName(method, i);
out.println(" params.put(\"" + paramName + "\", "
+ paramName + ");");
out.println(" params.put(\"" + paramName + "\", " + paramName + ");");
}

// Call the proxy
Expand All @@ -113,9 +103,9 @@ public <T> T createProxy(Class<? extends T> clazz, String url) {
} else {
factoryMethodName = "invokeAndDeserialize";
}
out.println(" return (" + method.getReturnType().getName()
+ ") proxy." + factoryMethodName + "(\"" + url
+ "\", \"" + method.getName() + "\", params);");
out.println(" return (" + method.getReturnType().getName() + ") proxy."
+ factoryMethodName + "(\"" + url + "\", \"" + method.getName()
+ "\", params);");
out.println(" }\n");
}
out.print("}\n");
Expand All @@ -126,17 +116,15 @@ public <T> T createProxy(Class<? extends T> clazz, String url) {

LOG.info("Compiling temp class");
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
Iterable<? extends JavaFileObject> compilationUnits = Arrays
.asList(file);
Iterable<? extends JavaFileObject> compilationUnits = Arrays.asList(file);

// Create temp directory if it doesn't exist.
File tempDir = new File("temp/");
if (!tempDir.exists()) {
tempDir.mkdir();
}
JavaCompiler.CompilationTask task = compiler.getTask(null, null,
diagnostics, Arrays.asList(new String[] { "-d", "temp" }),
null, compilationUnits);
JavaCompiler.CompilationTask task = compiler.getTask(null, null, diagnostics, Arrays
.asList(new String[] { "-d", "temp" }), null, compilationUnits);
boolean success = task.call();
if (success) {
LOG.info("Successfully compiled code");
Expand All @@ -149,8 +137,8 @@ public <T> T createProxy(Class<? extends T> clazz, String url) {
}

T instance = (T) fileClassLoader.createClass(
new File("temp/" + newClassName + ".class"))
.getConstructors()[0].newInstance(flatProxy);
new File("temp/" + newClassName + ".class")).getConstructors()[0]
.newInstance(flatProxy);
LOG.info("Successfully instantiated proxy");
return instance;
} catch (Exception e) {
Expand All @@ -167,9 +155,8 @@ private String extractParameterName(Method method, int parameterIndex) {
return ((ParamName) annotation).value();
}
}
LOG.warn("@" + ParamName.class.getSimpleName()
+ " annotation not present for method " + method.getName()
+ ", at index " + parameterIndex);
LOG.warn("@" + ParamName.class.getSimpleName() + " annotation not present for method "
+ method.getName() + ", at index " + parameterIndex);
return "param" + parameterIndex;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,17 @@
public class MockFlatProxy implements IFlatProxy {

@Override
public InputStream invoke(String url, String methodName,
Map<String, Object> params) {
public InputStream invoke(String url, String methodName, Map<String, Object> params) {
// InputStream stream = new ByteArrayInputStream(("{\"string\":\""
// + params.get("name").toString() + "\"}").getBytes());
XStream xstream = new XStream(new JettisonMappedXmlDriver());
xstream.setMode(XStream.NO_REFERENCES);
InputStream stream = new ByteArrayInputStream(xstream.toXML(
params.get("name")).getBytes());
InputStream stream = new ByteArrayInputStream(xstream.toXML(params.get("name")).getBytes());
return stream;
}

@Override
public Object invokeAndDeserialize(String url, String methodName,
Map<String, Object> params) {
public Object invokeAndDeserialize(String url, String methodName, Map<String, Object> params) {
return params.get("name");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,14 @@ public void setUp() throws Exception {

@Test
public void testCreateProxyString() {
ITestProxy proxy = factory.createProxy(ITestProxy.class,
"doesn't matter");
ITestProxy proxy = factory.createProxy(ITestProxy.class, "doesn't matter");
String name = "bancika";
assertEquals(name, proxy.test(name));
}

@Test
public void testCreateProxyStream() throws IOException {
ITestProxy proxy = factory.createProxy(ITestProxy.class,
"doesn't matter");
ITestProxy proxy = factory.createProxy(ITestProxy.class, "doesn't matter");
String name = "bancika";
InputStream stream = proxy.testStream(name);
byte[] buff = new byte[256];
Expand Down

0 comments on commit 914ac6c

Please sign in to comment.