Skip to content

Commit 193c799

Browse files
committed
Initial commit.
0 parents  commit 193c799

File tree

8 files changed

+381
-0
lines changed

8 files changed

+381
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.DS_Store
2+
node_modules/
3+
build/

.travis.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
language: node_js
2+
node_js:
3+
- '0.10'
4+
before_install:
5+
- npm install coffee-script
6+
before_script:
7+
- ./node_modules/.bin/cake archive
8+
script:
9+
- ./node_modules/.bin/cake test
10+
deploy:
11+
provider: releases
12+
api_key:
13+
secure: Bl2PKas/d4b6SAZ8qXgjhBsDZjbSNdfgF0TSlQ4HXOBAxyuV3k2g0svwrA3p1py7EpJWy5dqRJrnxUspsdq1W/mRGWRTHKEuTjq3YiQ4b+ElVIPCocMm0CEeEJ7lXff340du9cC6xUPwOtGP/1hLcumlkkZ147pxqmUyHGYMckw=
14+
file: build/JavaApacheHttpClientFluentAPICodeGenerator.zip
15+
skip_cleanup: true
16+
on:
17+
tags: true
18+
all_branches: true
19+
repo: LuckyMarmot/Paw-JavaApacheHttpClientFluentAPICodeGenerator

Cakefile

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
{spawn} = require 'child_process'
2+
{ncp} = require 'ncp'
3+
mkdirp = require 'mkdirp'
4+
fs = require 'fs'
5+
6+
file = 'JavaApacheHttpClientFluentAPICodeGenerator.coffee'
7+
identifier = 'com.luckymarmot.PawExtensions.JavaApacheHttpClientFluentAPICodeGenerator'
8+
9+
extensions_dir = "#{ process.env.HOME }/Library/Containers/com.luckymarmot.Paw/Data/Library/Application Support/com.luckymarmot.Paw/Extensions/"
10+
build_root_dir = "build"
11+
build_dir = "#{ build_root_dir }/#{ identifier }"
12+
13+
# compile CoffeeScript
14+
build_coffee = (callback) ->
15+
coffee = spawn 'coffee', ['-c', '-o', build_dir, file]
16+
coffee.stderr.on 'data', (data) ->
17+
process.stderr.write data.toString()
18+
coffee.stdout.on 'data', (data) ->
19+
process.stdout.write data.toString()
20+
coffee.on 'exit', (code) ->
21+
if code is 0
22+
callback?()
23+
else
24+
console.error "Build failed with error: #{ code }"
25+
26+
# copy files to build directory
27+
build_copy = () ->
28+
fs.writeFileSync "#{ build_dir }/README.md", fs.readFileSync("./README.md")
29+
fs.writeFileSync "#{ build_dir }/LICENSE", fs.readFileSync("./LICENSE")
30+
fs.writeFileSync "#{ build_dir }/java.mustache", fs.readFileSync("./java.mustache")
31+
fs.writeFileSync "#{ build_dir }/mustache.js", fs.readFileSync("./node_modules/mustache/mustache.js")
32+
33+
# build: build CoffeeScript and copy files to build directory
34+
build = (callback) ->
35+
# mkdir build dir
36+
mkdirp build_dir, (err) ->
37+
if err
38+
console.error err
39+
else
40+
build_coffee () ->
41+
build_copy()
42+
callback?()
43+
44+
# install: copy files to Extensions directory
45+
install = (callback) ->
46+
ncp build_dir, "#{ extensions_dir }/#{ identifier }", (err) ->
47+
if err
48+
console.error err
49+
else
50+
callback?()
51+
52+
# archive: create a zip archive from the build
53+
archive = (callback) ->
54+
zip_file = "#{ identifier.split('.').pop() }.zip"
55+
56+
# go to build dir
57+
process.chdir "#{ build_root_dir }/"
58+
59+
# delete any previous zip
60+
if fs.existsSync zip_file
61+
fs.unlinkSync zip_file
62+
63+
# zip
64+
zip = spawn 'zip', ["-r", zip_file, "#{ identifier }/"]
65+
zip.stderr.on 'data', (data) ->
66+
process.stderr.write data.toString()
67+
zip.stdout.on 'data', (data) ->
68+
process.stdout.write data.toString()
69+
zip.on 'exit', (code) ->
70+
if code is 0
71+
callback?()
72+
else
73+
console.error "zip returned with error code: #{ code }"
74+
75+
task 'build', ->
76+
build()
77+
78+
task 'test', ->
79+
build () ->
80+
# no test to run
81+
82+
task 'install', ->
83+
build () ->
84+
install()
85+
86+
task 'archive', ->
87+
build () ->
88+
archive()
89+
90+
task 'watch', ->
91+
# find all files in directory
92+
for filename in fs.readdirSync '.'
93+
# only watch non-hidden files
94+
if not filename.match(/^\./) and fs.lstatSync("./#{ filename }").isFile()
95+
fs.watchFile "./#{ filename }", {persistent:true, interval:500}, (_event, _filename) ->
96+
# when a file is changed, build and install
97+
build () ->
98+
install()
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
require "mustache.js"
2+
require "URI.js"
3+
4+
addslashes = (str) ->
5+
("#{str}").replace(/[\\"]/g, '\\$&')
6+
7+
multiLinesToSingleLine = (str) ->
8+
("#{str}").replace(/[\n"]/g, '\\n')
9+
10+
JavaApacheHttpClientFluentAPICodeGenerator = ->
11+
12+
@url = (request) ->
13+
return {
14+
"fullpath": request.url
15+
}
16+
17+
@headers = (request) ->
18+
headers = request.headers
19+
return {
20+
"has_headers": Object.keys(headers).length > 0
21+
"header_list": ({
22+
"header_name": addslashes header_name
23+
"header_value": addslashes header_value
24+
} for header_name, header_value of headers)
25+
}
26+
27+
@body = (request) ->
28+
json_body = request.jsonBody
29+
if json_body
30+
return {
31+
"has_json_body":true
32+
"json_body_object": @json_body_object json_body
33+
}
34+
35+
url_encoded_body = request.urlEncodedBody
36+
if url_encoded_body
37+
return {
38+
"has_url_encoded_body":true
39+
"url_encoded_body": ({
40+
"name": addslashes name
41+
"value": addslashes value
42+
} for name, value of url_encoded_body)
43+
}
44+
45+
multipart_body = request.multipartBody
46+
if multipart_body
47+
return {
48+
"has_multipart_body":true
49+
"multipart_body": ({
50+
"name": addslashes name
51+
"value": addslashes value
52+
} for name, value of multipart_body)
53+
}
54+
55+
raw_body = request.body
56+
if raw_body
57+
if raw_body.length < 5000
58+
has_tabs_or_new_lines = (null != /\r|\n|\t/.exec(raw_body))
59+
return {
60+
"has_raw_body":true
61+
"raw_body": if has_tabs_or_new_lines then multiLinesToSingleLine(addslashes raw_body) else addslashes raw_body
62+
}
63+
else
64+
return {
65+
"has_long_body":true
66+
}
67+
68+
@json_body_object = (object) ->
69+
if object == null
70+
s = "null"
71+
else if typeof(object) == 'string'
72+
s = "\\\"#{addslashes object}\\\""
73+
else if typeof(object) == 'number'
74+
s = "#{object}"
75+
else if typeof(object) == 'boolean'
76+
s = "#{if object then "true" else "false"}"
77+
else if typeof(object) == 'object'
78+
if object.length?
79+
s = '[' + ("#{@json_body_object(value)}" for value in object).join(',') + ']'
80+
else
81+
s = '{' + ("\\\"#{addslashes key}\\\": #{@json_body_object(value)}" for key, value of object).join(',') + '}'
82+
return s
83+
84+
@generate = (context) ->
85+
request = context.getCurrentRequest()
86+
87+
view =
88+
"request": context.getCurrentRequest()
89+
"method": request.method[0].toUpperCase() + request.method[1..-1].toLowerCase()
90+
"url": @url request
91+
"headers": @headers request
92+
"body": @body request
93+
94+
template = readFile "java.mustache"
95+
Mustache.render template, view
96+
97+
return
98+
99+
100+
JavaApacheHttpClientFluentAPICodeGenerator.identifier =
101+
"com.luckymarmot.PawExtensions.JavaApacheHttpClientFluentAPICodeGenerator";
102+
JavaApacheHttpClientFluentAPICodeGenerator.title =
103+
"Java (Apache HttpClient via Fluent API)";
104+
105+
registerCodeGenerator JavaApacheHttpClientFluentAPICodeGenerator

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2014 Paw Inc.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
[![Build Status](https://travis-ci.org/LuckyMarmot/Paw-JavaApacheHttpClientFluentAPICodeGenerator.svg?branch=master)](https://travis-ci.org/LuckyMarmot/Paw-JavaApacheHttpClientFluentAPICodeGenerator)
2+
3+
# Java + HttpClient (from HttpComponents) Code Generator (Paw Extension)
4+
5+
This a Paw Extension that generates code for Java using the [Apache HttpComponents Client](http://hc.apache.org/httpcomponents-client-ga/index.html) library via the [Fluent API](http://hc.apache.org/httpcomponents-client-ga/fluent-hc/apidocs/index.html).
6+
7+
## Installation
8+
9+
* `npm install`
10+
* `cake build`
11+
* `cake install`
12+
13+
## Tests
14+
15+
Download `HttpClient >= 4.3.x` at https://hc.apache.org/downloads.cgi
16+
17+
```bash
18+
export CLASSPATH=.:commons-logging-1.1.3.jar:httpmime-4.3.6.jar:httpcore-4.3.3.jar:httpclient-4.3.6.jar:fluent-hc-4.3.6.jar && javac SendRequest.java && java SendRequest
19+
```
20+
21+
## License
22+
23+
This Paw Extension is released under the [MIT License](LICENSE). Feel free to fork, and modify!
24+
25+
Copyright © 2014 Paw Inc.
26+
27+
## Contributors
28+
29+
Created by Quentin Rousseau ([@kwent](https://github.com/kwent)). See [Contributors](https://github.com/LuckyMarmot/Paw-JavaApacheHttpClientFluentAPICodeGenerator/graphs/contributors).
30+
31+
## Credits
32+
33+
* [Mustache.js](https://github.com/janl/mustache.js/), also released under the MIT License

java.mustache

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import java.io.IOException;
2+
import org.apache.http.client.fluent.*;
3+
{{#body}}
4+
import org.apache.http.entity.ContentType;
5+
{{/body}}
6+
{{#body.has_multipart_body}}
7+
import org.apache.http.HttpEntity;
8+
import org.apache.http.entity.mime.MultipartEntityBuilder;
9+
import org.apache.http.entity.mime.HttpMultipartMode;
10+
{{/body.has_multipart_body}}
11+
12+
public class SendRequest
13+
{
14+
public static void main(String[] args) {
15+
sendRequest();
16+
}
17+
18+
private static void sendRequest() {
19+
20+
// {{{request.name}}} ({{{request.method}}} {{{url.base}}})
21+
22+
try {
23+
{{! ----- }}
24+
{{#body.has_multipart_body}}
25+
HttpEntity entity = MultipartEntityBuilder.create()
26+
.setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
27+
{{#body.multipart_body}}
28+
.addTextBody("{{{name}}}", "{{{value}}}")
29+
{{/body.multipart_body}}
30+
.build();
31+
{{/body.has_multipart_body}}
32+
33+
// Create request
34+
Content content = Request.{{{method}}}("{{{url.fullpath}}}")
35+
{{#headers.has_headers}}
36+
37+
// Add headers
38+
{{#headers.header_list}}
39+
.addHeader("{{{header_name}}}", "{{{header_value}}}")
40+
{{/headers.header_list}}
41+
{{/headers.has_headers}}
42+
{{! ----- }}
43+
{{#body.has_raw_body}}
44+
45+
// Add body
46+
.bodyString("{{{body.raw_body}}}", ContentType.DEFAULT_TEXT)
47+
{{/body.has_raw_body}}
48+
{{! ----- }}
49+
{{#body.has_long_body}}
50+
51+
// Add body
52+
.bodyString("set your body string", ContentType.DEFAULT_TEXT)
53+
{{/body.has_long_body}}
54+
{{! ----- }}
55+
{{#body.has_url_encoded_body}}
56+
57+
// Add body
58+
.bodyForm(Form.form()
59+
{{#body.url_encoded_body}}
60+
.add("{{{name}}}", "{{{value}}}")
61+
{{/body.url_encoded_body}}
62+
.build())
63+
{{/body.has_url_encoded_body}}
64+
{{! ----- }}
65+
{{#body.has_multipart_body}}
66+
67+
// Add body
68+
.body(entity)
69+
{{/body.has_multipart_body}}
70+
{{! ----- }}
71+
{{#body.has_json_body}}
72+
73+
// Add body
74+
.bodyString("{{{body.json_body_object}}}", ContentType.APPLICATION_JSON)
75+
{{/body.has_json_body}}
76+
77+
// Fetch request and return content
78+
.execute().returnContent();
79+
80+
// Print content
81+
System.out.println(content);
82+
}
83+
catch (IOException e) { System.out.println(e); }
84+
}
85+
}

package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "Paw-JavaApacheHttpClientFluentAPICodeGenerator",
3+
"version": "1.0.0",
4+
"devDependencies": {
5+
"coffee-script": "latest",
6+
"mkdirp": "~0.5.0",
7+
"ncp": "~1.0.1"
8+
},
9+
"dependencies": {
10+
"mustache": "~0.8.2"
11+
},
12+
"repository": {
13+
"type": "git",
14+
"url": "[email protected]:LuckyMarmot/Paw-JavaApacheHttpClientFluentAPICodeGenerator.git"
15+
}
16+
}

0 commit comments

Comments
 (0)