Skip to content

Commit 1f85f39

Browse files
Merge remote-tracking branch 'origin/issue_9365'
2 parents 535d4b5 + 98d875d commit 1f85f39

25 files changed

+158
-171
lines changed

grails-codecs/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## grails-codecs
2+
3+
Code related to Grails codec methods (`.encodeAsHTML)`, `.encodeAsHex()`, etc...)

grails-codecs/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
dependencies {
2+
compile 'commons-codec:commons-codec:1.6'
3+
}

grails-plugin-codecs/src/main/groovy/org/grails/plugins/codecs/Base64Codec.groovy renamed to grails-codecs/src/main/groovy/org/grails/plugins/codecs/Base64CodecExtensionMethods.groovy

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@
1515
*/
1616
package org.grails.plugins.codecs
1717

18-
import org.codehaus.groovy.runtime.DefaultGroovyMethods
1918
import org.apache.commons.codec.binary.Base64
19+
import org.codehaus.groovy.runtime.NullObject
2020

2121
/**
2222
* A codec that encodes and decodes Objects using Base64 encoding.
2323
*
2424
* @author Drew Varner
2525
*/
26-
class Base64Codec {
26+
class Base64CodecExtensionMethods {
2727

28-
static encode = { theTarget ->
29-
if (theTarget == null) {
28+
static encodeAsBase64(theTarget) {
29+
if (theTarget == null || theTarget instanceof NullObject) {
3030
return null
3131
}
3232

@@ -37,8 +37,8 @@ class Base64Codec {
3737
return new String(Base64.encodeBase64(theTarget.toString().getBytes("UTF-8")))
3838
}
3939

40-
static decode = { theTarget ->
41-
if (theTarget == null) {
40+
static decodeBase64(theTarget) {
41+
if (theTarget == null || theTarget instanceof NullObject) {
4242
return null
4343
}
4444

grails-plugin-codecs/src/main/groovy/org/grails/plugins/codecs/HexCodec.groovy renamed to grails-codecs/src/main/groovy/org/grails/plugins/codecs/HexCodecExtensionMethods.groovy

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515
*/
1616
package org.grails.plugins.codecs
1717

18-
class HexCodec {
18+
import org.codehaus.groovy.runtime.NullObject
19+
20+
class HexCodecExtensionMethods {
1921

2022
static HEXDIGITS = '0123456789abcdef'
2123

2224
// Expects an array/list of numbers
23-
static encode = { theTarget ->
24-
if (theTarget == null) {
25+
static encodeAsHex(theTarget) {
26+
if (theTarget == null || theTarget instanceof NullObject) {
2527
return null
2628
}
2729

@@ -30,13 +32,13 @@ class HexCodec {
3032
theTarget = theTarget.getBytes("UTF-8")
3133
}
3234
theTarget.each() {
33-
result << HexCodec.HEXDIGITS[(it & 0xF0) >> 4]
34-
result << HexCodec.HEXDIGITS[it & 0x0F]
35+
result << HexCodecExtensionMethods.HEXDIGITS[(it & 0xF0) >> 4]
36+
result << HexCodecExtensionMethods.HEXDIGITS[it & 0x0F]
3537
}
3638
return result.toString()
3739
}
3840

39-
static decode = { theTarget ->
41+
static decodeHex(theTarget) {
4042
if (!theTarget) return null
4143

4244
def output = []

grails-plugin-codecs/src/main/groovy/org/grails/plugins/codecs/MD5Codec.groovy renamed to grails-codecs/src/main/groovy/org/grails/plugins/codecs/MD5BytesCodecExtensionMethods.groovy

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,19 @@
1515
*/
1616
package org.grails.plugins.codecs
1717

18-
class MD5Codec {
18+
import org.codehaus.groovy.runtime.NullObject
19+
20+
class MD5BytesCodecExtensionMethods {
1921
// Returns the byte[] of the digest, taken from UTF-8 of the string representation
2022
// or the raw data coerced to bytes
21-
static encode = { theTarget ->
22-
HexCodec.encode(MD5BytesCodec.encode(theTarget))
23+
static encodeAsMD5Bytes(theTarget) {
24+
if(theTarget == null || theTarget instanceof NullObject) {
25+
return null
26+
}
27+
DigestUtils.digest("MD5", theTarget)
2328
}
2429

25-
static decode = { theTarget ->
30+
static decodeMD5Bytes(theTarget) {
2631
throw new UnsupportedOperationException("Cannot decode MD5 hashes")
2732
}
2833
}

grails-plugin-codecs/src/main/groovy/org/grails/plugins/codecs/MD5BytesCodec.groovy renamed to grails-codecs/src/main/groovy/org/grails/plugins/codecs/MD5CodecExtensionMethods.groovy

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
*/
1616
package org.grails.plugins.codecs
1717

18-
class MD5BytesCodec {
18+
class MD5CodecExtensionMethods {
1919
// Returns the byte[] of the digest, taken from UTF-8 of the string representation
2020
// or the raw data coerced to bytes
21-
static encode = { theTarget ->
22-
DigestUtils.digest("MD5", theTarget)
21+
static encodeAsMD5(theTarget) {
22+
theTarget.encodeAsMD5Bytes()?.encodeAsHex()
2323
}
2424

25-
static decode = { theTarget ->
25+
static decodeMD5(theTarget) {
2626
throw new UnsupportedOperationException("Cannot decode MD5 hashes")
2727
}
2828
}

grails-plugin-codecs/src/main/groovy/org/grails/plugins/codecs/SHA1BytesCodec.groovy renamed to grails-codecs/src/main/groovy/org/grails/plugins/codecs/SHA1BytesCodecExtensionMethods.groovy

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,18 @@
1515
*/
1616
package org.grails.plugins.codecs
1717

18-
class SHA1BytesCodec {
18+
import org.codehaus.groovy.runtime.NullObject
19+
20+
class SHA1BytesCodecExtensionMethods {
1921
// Returns the byte[] of the digest
20-
static encode = { theTarget ->
22+
static encodeAsSHA1Bytes(theTarget) {
23+
if(theTarget == null || theTarget instanceof NullObject) {
24+
return null
25+
}
2126
DigestUtils.digest("SHA-1", theTarget)
2227
}
2328

24-
static decode = { theTarget ->
29+
static decodeSHA1Bytes(theTarget) {
2530
throw new UnsupportedOperationException("Cannot decode SHA-1 hashes")
2631
}
2732
}

grails-plugin-codecs/src/main/groovy/org/grails/plugins/codecs/SHA1Codec.groovy renamed to grails-codecs/src/main/groovy/org/grails/plugins/codecs/SHA1CodecExtensionMethods.groovy

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,18 @@
1515
*/
1616
package org.grails.plugins.codecs
1717

18-
class SHA1Codec{
18+
import org.codehaus.groovy.runtime.NullObject
19+
20+
class SHA1CodecExtensionMethods {
1921
// Returns the byte[] of the digest
20-
static encode = { theTarget ->
21-
HexCodec.encode(SHA1BytesCodec.encode(theTarget))
22+
static encodeAsSHA1(theTarget) {
23+
if(theTarget == null || theTarget instanceof NullObject) {
24+
return null
25+
}
26+
theTarget.encodeAsSHA1Bytes().encodeAsHex()
2227
}
2328

24-
static decode = { theTarget ->
29+
static decodeSHA1(theTarget) {
2530
throw new UnsupportedOperationException("Cannot decode SHA-1 hashes")
2631
}
2732
}

grails-plugin-codecs/src/main/groovy/org/grails/plugins/codecs/SHA256BytesCodec.groovy renamed to grails-codecs/src/main/groovy/org/grails/plugins/codecs/SHA256BytesCodecExtensionMethods.groovy

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,18 @@
1515
*/
1616
package org.grails.plugins.codecs
1717

18-
class SHA256BytesCodec {
18+
import org.codehaus.groovy.runtime.NullObject
19+
20+
class SHA256BytesCodecExtensionMethods {
1921
// Returns the byte[] of the digest
20-
static encode = { theTarget ->
22+
static encodeAsSHA256Bytes(theTarget) {
23+
if(theTarget == null || theTarget instanceof NullObject) {
24+
return null
25+
}
2126
DigestUtils.digest("SHA-256", theTarget)
2227
}
2328

24-
static decode = { theTarget ->
29+
static decodeSHA256Bytes(theTarget) {
2530
throw new UnsupportedOperationException("Cannot decode SHA-256 hashes")
2631
}
2732
}

0 commit comments

Comments
 (0)