Skip to content

Commit ae2bdb2

Browse files
committed
Add web and swing samples
1 parent 178f24a commit ae2bdb2

38 files changed

+2951
-9
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,5 @@ node_modules/
7575
.DS_Store
7676
.DS_Store?
7777

78-
*vmware*
78+
**/vmware/*
79+
**/internal/*

java/misc/src/main/java/Merge.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import java.util.*;
2+
import java.io.*;
3+
4+
public class Merge {
5+
public static void main(String[] args) throws Exception {
6+
String dataPath = args[0];
7+
String metadataPath = args[1];
8+
String outputPath = args[2];
9+
Map<String, Map<String, String>> metadata = readAndIndexMetadata(metadataPath);
10+
convertData(dataPath, outputPath, metadata);
11+
}
12+
13+
private static Map<String, Map<String, String>> readAndIndexMetadata(String metadataPath) throws Exception {
14+
Map<String, Map<String, String>> result = new HashMap<>();
15+
BufferedReader reader = new BufferedReader(new FileReader(new File(metadataPath)));
16+
String line = "";
17+
while ((line = reader.readLine()) != null) {
18+
String[] values = line.split(",");
19+
if (!result.containsKey(values[0])) {
20+
result.put(values[0], new HashMap<String, String>());
21+
}
22+
result.get(values[0]).put(values[1], values[2]);
23+
}
24+
reader.close();
25+
return result;
26+
}
27+
28+
private static void convertData(String dataPath, String outputPath, Map<String, Map<String, String>> metadata) throws Exception {
29+
BufferedReader reader = new BufferedReader(new FileReader(new File(dataPath)));
30+
BufferedWriter writer = new BufferedWriter(new FileWriter(new File(outputPath)));
31+
String line = "";
32+
while ((line = reader.readLine()) != null) {
33+
String[] values = line.split(",");
34+
StringBuilder result = new StringBuilder("");
35+
result.append(metadata.get("AWS-Billing-Account").get(values[0])).append(",");
36+
result.append(metadata.get("AWS-Account").get(values[1])).append(",");
37+
result.append(metadata.get("AWS-Service-Category").get(values[2])).append(",");
38+
result.append(metadata.get("Past-12-Months").get(values[3])).append(",");
39+
result.append(values[4]);
40+
writer.write(result.toString());
41+
}
42+
writer.flush();
43+
writer.close();
44+
reader.close();
45+
}
46+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.openapex.samples.basic;
2+
3+
public class BitwiseOperations {
4+
public static void main(String[] args) {
5+
long num = 1026832071211166612L;
6+
for(int i = 0; i < 64; i++){
7+
boolean set = (num & (1L<<i))>0;
8+
System.out.println("Bit "+i+ "set?: "+set);
9+
}
10+
//System.out.println(num & (2L<<34));
11+
}
12+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.openapex.samples.basic;
2+
3+
public class DataType {
4+
public static void main(String[] args) {
5+
double a = 12.34d;
6+
float b = 1.3f;
7+
long c = 120l;
8+
System.out.println(a+", "+b+", "+c);
9+
}
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.openapex.samples.basic;
2+
3+
public class FilePathEscape {
4+
public static void main(String[] args) {
5+
String path1 = "E:\test\filename.png";
6+
String path2 = "E:\\test\\filename.png";
7+
System.out.println("Path1: " + path1);
8+
System.out.println("Path2: " + path2);
9+
}
10+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package org.openapex.samples.basic;
2+
3+
import java.util.*;
4+
import java.io.*;
5+
6+
public class Merge{
7+
public static void main(String[] args) throws Exception{
8+
String dataPath = args[0];
9+
String metadataPath = args[1];
10+
String outputPath = args[2];
11+
Map<String, Map<String, String>> metadata = readAndIndexMetadata(metadataPath);
12+
convertData(dataPath, outputPath, metadata);
13+
}
14+
15+
private static Map<String, Map<String, String>> readAndIndexMetadata(String metadataPath) throws Exception{
16+
Map<String, Map<String, String>> result = new HashMap<>();
17+
BufferedReader reader = new BufferedReader(new FileReader(new File(metadataPath)));
18+
String line = "";
19+
while ((line= reader.readLine())!=null){
20+
String[] values = line.split(",");
21+
if(!result.containsKey(values[0])){
22+
result.put(values[0], new HashMap<String, String>());
23+
}
24+
result.get(values[0]).put(values[1], values[2]);
25+
}
26+
reader.close();
27+
return result;
28+
}
29+
30+
private static void convertData(String dataPath, String outputPath, Map<String, Map<String, String>> metadata) throws Exception{
31+
BufferedReader reader = new BufferedReader(new FileReader(new File(dataPath)));
32+
BufferedWriter writer = new BufferedWriter(new FileWriter(new File(outputPath)));
33+
String line = "";
34+
System.out.println(metadata);
35+
writer.write("AWS-Billing-Account,AWS-Account,AWS-Service-Category,Month,cost_before_chargeback\n");
36+
while ((line= reader.readLine())!=null){
37+
if(line.startsWith("AWS-Billing-Account")){
38+
continue;
39+
}
40+
String[] values = line.split(",");
41+
if(values.length < 4){
42+
System.out.println("Invalid entry: "+line);
43+
}
44+
StringBuilder result = new StringBuilder("");
45+
result.append(metadata.get("AWS-Billing-Account").get(values[0])).append(",");
46+
result.append(metadata.get("AWS-Account").get(values[1])).append(",");
47+
result.append(metadata.get("AWS-Service-Category").get(values[2])).append(",");
48+
result.append(metadata.get("Past-12-Months").get(values[3])).append(",");
49+
result.append(values[4]);
50+
writer.write(result.toString()+"\n");
51+
}
52+
writer.flush();
53+
writer.close();
54+
reader.close();
55+
}
56+
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package org.openapex.samples.misc.http;
2+
3+
import javax.net.ssl.HttpsURLConnection;
4+
import java.io.IOException;
5+
import java.io.InputStream;
6+
import java.net.HttpURLConnection;
7+
import java.net.URL;
8+
import java.util.Scanner;
9+
10+
/**
11+
* HttpClient class is capable to invoke URL, receive response
12+
* from server and process data.
13+
* @author mrityunjoy_saha
14+
* @version 1.0
15+
* @since Apex 1.2
16+
*/
17+
public class HttpClientWithProxy {
18+
19+
/**
20+
* Creates a new instance of {@code HttpClient}.
21+
* @param proxyConnection Indicates whether or nor proxy connection to be used.
22+
*/
23+
public HttpClientWithProxy(boolean proxyConnection) {
24+
// In case proxy connection is required set additional connection properties.
25+
if (proxyConnection) {
26+
setupProxy();
27+
}
28+
}
29+
30+
/**
31+
* Sets proxy connection related properties.
32+
*/
33+
private void setupProxy() {
34+
System.out.println("Setting up proxy configuration");
35+
// Http proxy properties
36+
System.setProperty("https.proxyHost", "10.152.45.85");
37+
System.setProperty("https.proxyPort", "3128");
38+
// User name and password are optional
39+
System.setProperty("https.proxyUser", "pdhaval@ntlm.com");
40+
System.setProperty("https.proxyPassword", "VMware1!");
41+
}
42+
43+
/**
44+
* Main method of the program.
45+
* @param args Input arguments to the program.
46+
*/
47+
public static void main(String[] args) {
48+
HttpClientWithProxy handler = new HttpClientWithProxy(true);
49+
Scanner scanner = new Scanner(System.in);
50+
while(true) {
51+
System.out.println("URL (q to quit):");
52+
String url = scanner.nextLine();
53+
if(!url.equalsIgnoreCase("q")) {
54+
InputStream response = handler.sendRequest(url.trim());
55+
handler.processResponse(response);
56+
} else {
57+
break;
58+
}
59+
}
60+
}
61+
62+
/**
63+
* Opens a connection and sends Http request to given URL.
64+
* @param url URL to be invoked.
65+
* @return Response as a stream.
66+
*/
67+
private InputStream sendRequest(String url) {
68+
try {
69+
URL urlInstance = new URL(url);
70+
HttpsURLConnection connection = (HttpsURLConnection) urlInstance.
71+
openConnection();
72+
// Set timeout in milliseconds
73+
connection.setConnectTimeout(20000);
74+
// Set request headers
75+
addRequestHeaders(connection);
76+
// Establiish a connection
77+
connection.connect();
78+
System.out.println("Using Proxy? "+connection.usingProxy());
79+
// Read response status and headers
80+
readResponseStatusAndHeaders(connection);
81+
// Return response
82+
return connection.getInputStream();
83+
} catch (IOException ex) {
84+
ex.printStackTrace();
85+
}
86+
return null;
87+
}
88+
89+
/**
90+
* Adds request headers to connection.
91+
* @param connection Http URL connection instance.
92+
*/
93+
private void addRequestHeaders(HttpURLConnection connection) {
94+
connection.addRequestProperty("Accept", "text/plain");
95+
connection.addRequestProperty("Accept-Language", "en");
96+
connection.addRequestProperty("Cache-Control", "no-cache");
97+
}
98+
99+
/**
100+
* Reads response code, message and header fields.
101+
* @param connection Http URL connection instance.
102+
* @throws IOException If an error occrred while connecting to server.
103+
*/
104+
private void readResponseStatusAndHeaders(HttpURLConnection connection) throws
105+
IOException {
106+
System.out.println("Response code: " + connection.getResponseCode());
107+
System.out.println(
108+
"Response message: " + connection.getResponseMessage());
109+
System.out.println("Response header: Content-Length: " + connection.
110+
getHeaderField("Content-Length"));
111+
}
112+
113+
/**
114+
* Prints the response to console.
115+
* @param response Response stream.
116+
*/
117+
private void processResponse(InputStream response) {
118+
if (response == null) {
119+
System.out.println("No or blank response received");
120+
} else {
121+
StringBuilder data = new StringBuilder();
122+
try {
123+
int bufferSize = 2048;
124+
byte[] inputData = new byte[bufferSize];
125+
int count = 0;
126+
while ((count = response.read(inputData, 0, bufferSize)) != -1) {
127+
data.append(new String(inputData, 0, count, "UTF-8"));
128+
}
129+
} catch (IOException ex) {
130+
ex.printStackTrace();
131+
}
132+
System.out.println("Data received: " + data.toString());
133+
}
134+
}
135+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package org.openapex.samples.misc.swing;
2+
3+
import javax.swing.JFrame;
4+
import javax.swing.JLabel;
5+
import javax.swing.JPanel;
6+
import javax.swing.JTextField;
7+
import javax.swing.SwingUtilities;
8+
import javax.swing.text.AbstractDocument;
9+
import javax.swing.text.AttributeSet;
10+
import javax.swing.text.BadLocationException;
11+
import javax.swing.text.DocumentFilter;
12+
13+
/**
14+
* NumericTextField allows only numbers in the text field and not even decimal
15+
* points. Also, it restricts the length of input.
16+
*
17+
* @version 1.0
18+
* @since Apex 1.2
19+
*/
20+
public class NumericTextField {
21+
22+
/**
23+
*
24+
*/
25+
public void createUI() {
26+
final JFrame frame = new JFrame("NumericTextField");
27+
frame.setSize(400, 400);
28+
JPanel panel = new JPanel();
29+
JLabel label = new JLabel("Enter some input (only numbers are allowed of max length 10):");
30+
final JTextField textField = new JTextField(30);
31+
// Add the document filter to text field for numeric and length check.
32+
((AbstractDocument) textField.getDocument()).setDocumentFilter(new NumericAndLengthFilter(10));
33+
panel.add(label);
34+
panel.add(textField);
35+
frame.getContentPane().add(panel);
36+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
37+
frame.setVisible(true);
38+
}
39+
40+
/**
41+
* Entry point of the application.
42+
*
43+
* @param args Input arguments.
44+
*/
45+
public static void main(String[] args) {
46+
final NumericTextField test = new NumericTextField();
47+
SwingUtilities.invokeLater(new Runnable() {
48+
49+
public void run() {
50+
test.createUI();
51+
}
52+
});
53+
}
54+
55+
/**
56+
* A document filter for numeric and length check.
57+
*/
58+
private class NumericAndLengthFilter extends DocumentFilter {
59+
60+
/**
61+
* Number of characters allowed.
62+
*/
63+
private int length = 0;
64+
65+
/**
66+
* Restricts the number of charcacters can be entered by given length.
67+
*
68+
* @param length Number of characters allowed.
69+
*/
70+
public NumericAndLengthFilter(int length) {
71+
this.length = length;
72+
}
73+
74+
@Override
75+
public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr)
76+
throws BadLocationException {
77+
if (isNumeric(string)) {
78+
if (this.length > 0 && fb.getDocument().getLength() + string.length() > this.length) {
79+
return;
80+
}
81+
super.insertString(fb, offset, string, attr);
82+
}
83+
}
84+
85+
@Override
86+
public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs)
87+
throws BadLocationException {
88+
if (isNumeric(text)) {
89+
if (this.length > 0 && fb.getDocument().getLength() + text.length() > this.length) {
90+
return;
91+
}
92+
super.insertString(fb, offset, text, attrs);
93+
}
94+
}
95+
96+
/**
97+
* This method tests whether given text can be represented as number. This
98+
* method can be enhanced further for specific needs.
99+
*
100+
* @param text Input text.
101+
* @return {@code true} if given string can be converted to number; otherwise
102+
* returns {@code false}.
103+
*/
104+
private boolean isNumeric(String text) {
105+
if (text == null || text.trim().equals("")) {
106+
return false;
107+
}
108+
for (int iCount = 0; iCount < text.length(); iCount++) {
109+
if (!Character.isDigit(text.charAt(iCount))) {
110+
return false;
111+
}
112+
}
113+
return true;
114+
}
115+
}
116+
}

0 commit comments

Comments
 (0)