Frida: Replace string literal in class method - frida

I ran an APK through dex2jar and JD-GUI and found the class I'm interested in:
public class RESTClient {
....
private bdG.\u0269 \u02CB\u0971() {
return (new bdG.\u0269()).\u02CA((new bdq.if()).\u0971("www.google.com", new String[] { "sha256/asdIa1tHg96AnzarJ6GJLu6JiogJla3UDsPWMDICs=" }).\u02CE()).\u02CE((bdF)\u02BD\u0971()).\u02CB((bdv)\u02BC\u0971());
}
}
I've been trying to use Frida to replace the sha256/... string but have been unsuccessful thus far.
Here's what I've done:
Java.perform(function() {
var StringBuilder = Java.use('java.lang.StringBuilder');
StringBuilder.$init.overload('java.lang.String').implementation = function(str) {
return this.$init.call(this, str);
}
StringBuilder.toString.implementation = function() {
var result = this.toString.call(this);
if(result == "sha256/asdIa1tHg96AnzarJ6GJLu6JiogJla3UDsPWMDICs=") {
console.log("[x] Found and replaced!");
return "sha256/somethingelsegoeshere";
}
return result;
}
});
While this code does match, the returned string doesn't appear to have any effect. How can I find and replace the string in memory directly?
EDIT: Including jadx output too:
private C9575bdG.C4644 m708() {
return new C9575bdG.C4644().m73741(new C9612bdq.Cif().m74142("www.google.com", "sha256/asdIa1tHg96AnzarJ6GJLu6JiogJla3UDsPWMDICs=").m74141()).m73751((C9574bdF) m695()).m73747((C9617bdv) m694());
}
EDIT: Including smali output from apktool:
.method private ˋॱ()Lo/bdG$ɩ;
.locals 6
.prologue
.line 99
new-instance v0, Lo/bdG$ɩ;
invoke-direct {v0}, Lo/bdG$ɩ;-><init>()V
new-instance v1, Lo/bdq$if;
invoke-direct {v1}, Lo/bdq$if;-><init>()V
const-string v2, "www.google.com"
const/4 v3, 0x1
new-array v3, v3, [Ljava/lang/String;
const/4 v4, 0x0
const-string v5, "sha256/asdIa1tHg96AnzarJ6GJLu6JiogJla3UDsPWMDICs="
aput-object v5, v3, v4
.line 100
invoke-virtual {v1, v2, v3}, Lo/bdq$if;->ॱ(Ljava/lang/String;[Ljava/lang/String;)Lo/bdq$if;
move-result-object v1
invoke-virtual {v1}, Lo/bdq$if;->ˎ()Lo/bdq;
move-result-object v1
invoke-virtual {v0, v1}, Lo/bdG$ɩ;->ˊ(Lo/bdq;)Lo/bdG$ɩ;
move-result-object v0
.line 101
invoke-direct {p0}, Lcom/target/android/data/remote/RESTClient;->ʽॱ()Lo/bfa;
move-result-object v1
invoke-virtual {v0, v1}, Lo/bdG$ɩ;->ˎ(Lo/bdF;)Lo/bdG$ɩ;
move-result-object v0
.line 102
invoke-direct {p0}, Lcom/target/android/data/remote/RESTClient;->ʼॱ()Lo/qz;
move-result-object v1
invoke-virtual {v0, v1}, Lo/bdG$ɩ;->ˋ(Lo/bdv;)Lo/bdG$ɩ;
move-result-object v0
.line 99
return-object v0
.end method

This call in Java:
.\u0971("www.google.com", new String[] {"sha256/asdIa1tHg96AnzarJ6GJLu6JiogJla3UDsPWMDICs=" })
is found in smali at:
invoke-virtual {v1, v2, v3}, Lo/bdq$if;->ॱ(Ljava/lang/String;[Ljava/lang/String;)Lo/bdq$if;
The Java class is o.bdq$if (class if is nested inside o.bdq). The method name is ॱ
Java.perform(function() {
var ObfuscatedClass = Java.use('o.bdq$if');
ObfuscatedClass.ॱ.implementation = function(string, stringArray) { // replace original implementation
var modifiedStringArray = ...; // do your stuff with the stringArray that contains your hash
return this.ॱ(string, modifiedStringArray); // call original method with modified string array containing new hash
}
});

Related

Dynamically parse yaml field to one of a finite set of structs in Go

I have a yaml file, where one field could be represented by one of possible kinds of structs. To simplify the code and yaml files, let's say I have these yaml files:
kind: "foo"
spec:
fooVal: 4
kind: "bar"
spec:
barVal: 5
And these structs for parsing:
type Spec struct {
Kind string `yaml:"kind"`
Spec interface{} `yaml:"spec"`
}
type Foo struct {
FooVal int `yaml:"fooVal"`
}
type Bar struct {
BarVal int `yaml:"barVal"`
}
I know that I can use map[string]interface{} as a type of Spec field. But the real example is more complex, and involves more possible struct types, not only Foo and Bar, this is why I don't like to parse spec into the field.
I've found a workaround for this: unmarshal the yaml into intermediate struct, then check kind field, and marshal map[string]interface{} field into yaml back, and unmarshal it into concrete type:
var spec Spec
if err := yaml.Unmarshal([]byte(src), &spec); err != nil {
panic(err)
}
tmp, _ := yaml.Marshal(spec.Spec)
if spec.Kind == "foo" {
var foo Foo
yaml.Unmarshal(tmp, &foo)
fmt.Printf("foo value is %d\n", foo.FooVal)
}
if spec.Kind == "bar" {
tmp, _ := yaml.Marshal(spec.Spec)
var bar Bar
yaml.Unmarshal(tmp, &bar)
fmt.Printf("bar value is %d\n", bar.BarVal)
}
But it requires additional step and consumes more memory (real yaml file could be bigger than in examples). Does some more elegant way exist to unmarshal yaml dynamically into a finite set of structs?
Update: I'm using github.com/go-yaml/yaml v2.1.0 Yaml parser.
For use with yaml.v2 you can do the following:
type yamlNode struct {
unmarshal func(interface{}) error
}
func (n *yamlNode) UnmarshalYAML(unmarshal func(interface{}) error) error {
n.unmarshal = unmarshal
return nil
}
type Spec struct {
Kind string `yaml:"kind"`
Spec interface{} `yaml:"-"`
}
func (s *Spec) UnmarshalYAML(unmarshal func(interface{}) error) error {
type S Spec
type T struct {
S `yaml:",inline"`
Spec yamlNode `yaml:"spec"`
}
obj := &T{}
if err := unmarshal(obj); err != nil {
return err
}
*s = Spec(obj.S)
switch s.Kind {
case "foo":
s.Spec = new(Foo)
case "bar":
s.Spec = new(Bar)
default:
panic("kind unknown")
}
return obj.Spec.unmarshal(s.Spec)
}
https://play.golang.org/p/Ov0cOaedb-x
For use with yaml.v3 you can do the following:
type Spec struct {
Kind string `yaml:"kind"`
Spec interface{} `yaml:"-"`
}
func (s *Spec) UnmarshalYAML(n *yaml.Node) error {
type S Spec
type T struct {
*S `yaml:",inline"`
Spec yaml.Node `yaml:"spec"`
}
obj := &T{S: (*S)(s)}
if err := n.Decode(obj); err != nil {
return err
}
switch s.Kind {
case "foo":
s.Spec = new(Foo)
case "bar":
s.Spec = new(Bar)
default:
panic("kind unknown")
}
return obj.Spec.Decode(s.Spec)
}
https://play.golang.org/p/ryEuHyU-M2Z
You can do this by implementing a custom UnmarshalYAML func. However, with the v2 version of the API, you would basically do the same thing as you do now and just encapsulate it a bit better.
If you switch to using the v3 API however, you get a better UnmarshalYAML that actually lets you work on the parsed YAML node before it is processed into a native Go type. Here's how that looks:
package main
import (
"errors"
"fmt"
"gopkg.in/yaml.v3"
)
type Spec struct {
Kind string `yaml:"kind"`
Spec interface{} `yaml:"spec"`
}
type Foo struct {
FooVal int `yaml:"fooVal"`
}
type Bar struct {
BarVal int `yaml:"barVal"`
}
func (s *Spec) UnmarshalYAML(value *yaml.Node) error {
s.Kind = ""
for i := 0; i < len(value.Content)/2; i += 2 {
if value.Content[i].Kind == yaml.ScalarNode &&
value.Content[i].Value == "kind" {
if value.Content[i+1].Kind != yaml.ScalarNode {
return errors.New("kind is not a scalar")
}
s.Kind = value.Content[i+1].Value
break
}
}
if s.Kind == "" {
return errors.New("missing field `kind`")
}
switch s.Kind {
case "foo":
var foo Foo
if err := value.Decode(&foo); err != nil {
return err
}
s.Spec = foo
case "bar":
var bar Bar
if err := value.Decode(&bar); err != nil {
return err
}
s.Spec = bar
default:
return errors.New("unknown kind: " + s.Kind)
}
return nil
}
var input1 = []byte(`
kind: "foo"
spec:
fooVal: 4
`)
var input2 = []byte(`
kind: "bar"
spec:
barVal: 5
`)
func main() {
var s1, s2 Spec
if err := yaml.Unmarshal(input1, &s1); err != nil {
panic(err)
}
fmt.Printf("Type of spec from input1: %T\n", s1.Spec)
if err := yaml.Unmarshal(input2, &s2); err != nil {
panic(err)
}
fmt.Printf("Type of spec from input2: %T\n", s2.Spec)
}
I suggest looking into the possibility of using YAML tags instead of your current structure to model this in your YAML; tags have been designed exactly for this purpose. Instead of the current YAML
kind: "foo"
spec:
fooVal: 4
you could write
--- !foo
fooVal: 4
Now you don't need the describing structure with kind and spec anymore. Loading this would look a bit different as you'd need a wrapping root type you can define UnmarshalYAML on, but it may be feasible if this is just a part of a larger structure. You can access the tag !foo in the yaml.Node's Tag field.

How to search for text in a binary file in Dart

I am trying to search the binary data of a file in dart, to find the index of a substring. I have working js code but I am unable to convert it to dart. This is the js snippet:
var rp = require('request-promise');
async function test(){
const uri = "https://file-examples-com.github.io/uploads/2017/04/file_example_MP4_480_1_5MG.mp4"
const result = await rp({ uri });
const position = Buffer.from(result).indexOf('dnlu');
console.log(position);
}
test() //outputs 2631582
What would be the dart equivalent of this function?
You'll typically fetch the bytes as a Uint8List.
The Dart Uint8List does not have an indexOf method which works on sublists, so you'll have to search the old fashioned way - by looking at it.
I assume that the bytes represent UTF-8 or Latin-1 characters, and since your string contains only ASCII, you can search for the code units directly.
Maybe you could add something like:
extension IndexOfListExtension<T> on List<T> {
int indexOfAll(List<T> needle, [int start = 0]) {
if (needle.length == 0) return start;
var first = needle[0];
var end = this.length - needle.length;
for (var i = start; i <= end; i++) {
match:
if (this[i] == first) {
for (var j = 1; j < needle.length; j++) {
if (this[i + j] != needle[j]) break match;
}
return i;
}
}
return -1;
}
}
Then you would be able to do:
var bytes = await fetch_the_bytes(uri); // However you want to do this.
var position = bytes.indexOfAll("dnlu".codeUnits);
...

Mp4 cannot play on iOS by request spring mvc server resource, but working on Nginx access mp4 file directly

I have two scenario when play mp4 file on iOS devices.
MP4 access by Nginx is working:
Put mp4 file in /html and using following configure, then iOS devices and chrome browser can play mp4 files properly.
server {
listen 127.0.0.1:80 default_server;
location ~ ^/storage\/*.mp4 {
root html;
}
}
MP4 access by Tomcat Spring MVC is not working: When I request by Spring mvc restful API and return ResponseEntity contains Resource Object, in chrome browser will receive and play mp4 properly. But iOS devices not working
#GetMapping(value = "/storage/{filename:.+}")
#ResponseBody
public ResponseEntity<org.springframework.core.io.Resource> accessStorageFile(HttpServletResponse response, #PathVariable String filename) throws IOException {
org.springframework.core.io.Resource resource = storageUtil.loadAsResource(filename);
InputStream inputStream = resource.getInputStream();
InputStreamResource inputStreamResource = new InputStreamResource(inputStream);
String contentType = FileTypeMap.getDefaultFileTypeMap().getContentType(resource.getFile());
contentType = resource.getFilename().contains(".mp4") ? "video/mp4" : contentType;
response.setContentType(contentType);
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setContentType(MediaType.valueOf(contentType));
responseHeaders.setContentLength(resource.getFile().length());
return new ResponseEntity<>(inputStreamResource, responseHeaders, HttpStatus.OK);
}
import java.io.BufferedInputStream; import java.io.File; import
java.io.IOException; import java.io.InputStream; import
java.io.OutputStream; import java.nio.file.Files; import
java.nio.file.Path; import java.nio.file.Paths; import
java.nio.file.attribute.FileTime; import java.time.LocalDateTime;
import java.time.ZoneId; import java.time.ZoneOffset; import
java.util.ArrayList; import java.util.Arrays; import java.util.List;
import javax.servlet.ServletOutputStream; import
javax.servlet.http.HttpServletRequest; import
javax.servlet.http.HttpServletResponse; import
org.springframework.util.StringUtils;
/** * * #author David 1 */ public class MultipartFileSender {
private static final int DEFAULT_BUFFER_SIZE = 20480; // ..bytes = 20KB.
private static final long DEFAULT_EXPIRE_TIME = 604800000L; // ..ms = 1 week.
private static final String MULTIPART_BOUNDARY = "MULTIPART_BYTERANGES";
Path filepath;
HttpServletRequest request;
HttpServletResponse response;
public MultipartFileSender() {
}
public static MultipartFileSender fromPath(Path path) {
return new MultipartFileSender().setFilepath(path);
}
public static MultipartFileSender fromFile(File file) {
return new MultipartFileSender().setFilepath(file.toPath());
}
public static MultipartFileSender fromURIString(String uri) {
return new MultipartFileSender().setFilepath(Paths.get(uri));
}
//** internal setter **//
private MultipartFileSender setFilepath(Path filepath) {
this.filepath = filepath;
return this;
}
public MultipartFileSender with(HttpServletRequest httpRequest) {
request = httpRequest;
return this;
}
public MultipartFileSender with(HttpServletResponse httpResponse) {
response = httpResponse;
return this;
}
public void serveResource() throws Exception {
if (response == null || request == null) {
return;
}
if (!Files.exists(filepath)) {
System.out.println("File doesn't exist at URI : {" + filepath.toAbsolutePath().toString() + "}");
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
Long length = Files.size(filepath);
String fileName = filepath.getFileName().toString();
FileTime lastModifiedObj = Files.getLastModifiedTime(filepath);
if (StringUtils.isEmpty(fileName) || lastModifiedObj == null) {
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
}
long lastModified = LocalDateTime.ofInstant(lastModifiedObj.toInstant(),
ZoneId.of(ZoneOffset.systemDefault().getId())).toEpochSecond(ZoneOffset.UTC);
String contentType = "video/mp4";
// Validate request headers for caching ---------------------------------------------------
// If-None-Match header should contain "*" or ETag. If so, then return 304.
String ifNoneMatch = request.getHeader("If-None-Match");
if (ifNoneMatch != null && HttpUtils.matches(ifNoneMatch, fileName)) {
response.setHeader("ETag", fileName); // Required in 304.
response.sendError(HttpServletResponse.SC_NOT_MODIFIED);
return;
}
// If-Modified-Since header should be greater than LastModified. If so, then return 304.
// This header is ignored if any If-None-Match header is specified.
long ifModifiedSince = request.getDateHeader("If-Modified-Since");
if (ifNoneMatch == null && ifModifiedSince != -1 && ifModifiedSince + 1000 > lastModified) {
response.setHeader("ETag", fileName); // Required in 304.
response.sendError(HttpServletResponse.SC_NOT_MODIFIED);
return;
}
// Validate request headers for resume ----------------------------------------------------
// If-Match header should contain "*" or ETag. If not, then return 412.
String ifMatch = request.getHeader("If-Match");
if (ifMatch != null && !HttpUtils.matches(ifMatch, fileName)) {
response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
return;
}
// If-Unmodified-Since header should be greater than LastModified. If not, then return 412.
long ifUnmodifiedSince = request.getDateHeader("If-Unmodified-Since");
if (ifUnmodifiedSince != -1 && ifUnmodifiedSince + 1000 <= lastModified) {
response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
return;
}
// Validate and process range -------------------------------------------------------------
// Prepare some variables. The full Range represents the complete file.
Range full = new Range(0, length - 1, length);
List<Range> ranges = new ArrayList<>();
// Validate and process Range and If-Range headers.
String range = request.getHeader("Range");
if (range != null) {
// Range header should match format "bytes=n-n,n-n,n-n...". If not, then return 416.
if (!range.matches("^bytes=\\d*-\\d*(,\\d*-\\d*)*$")) {
response.setHeader("Content-Range", "bytes */" + length); // Required in 416.
response.sendError(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE);
return;
}
String ifRange = request.getHeader("If-Range");
if (ifRange != null && !ifRange.equals(fileName)) {
try {
long ifRangeTime = request.getDateHeader("If-Range"); // Throws IAE if invalid.
if (ifRangeTime != -1) {
ranges.add(full);
}
} catch (IllegalArgumentException ignore) {
ranges.add(full);
}
}
// If any valid If-Range header, then process each part of byte range.
if (ranges.isEmpty()) {
for (String part : range.substring(6).split(",")) {
// Assuming a file with length of 100, the following examples returns bytes at:
// 50-80 (50 to 80), 40- (40 to length=100), -20 (length-20=80 to length=100).
long start = Range.sublong(part, 0, part.indexOf("-"));
long end = Range.sublong(part, part.indexOf("-") + 1, part.length());
if (start == -1) {
start = length - end;
end = length - 1;
} else if (end == -1 || end > length - 1) {
end = length - 1;
}
// Check if Range is syntactically valid. If not, then return 416.
if (start > end) {
response.setHeader("Content-Range", "bytes */" + length); // Required in 416.
response.sendError(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE);
return;
}
// Add range.
ranges.add(new Range(start, end, length));
}
}
}
// Prepare and initialize response --------------------------------------------------------
// Get content type by file name and set content disposition.
String disposition = "inline";
// If content type is unknown, then set the default value.
// For all content types, see: http://www.w3schools.com/media/media_mimeref.asp
// To add new content types, add new mime-mapping entry in web.xml.
if (contentType == null) {
contentType = "application/octet-stream";
} else if (!contentType.startsWith("image")) {
// Else, expect for images, determine content disposition. If content type is supported by
// the browser, then set to inline, else attachment which will pop a 'save as' dialogue.
String accept = request.getHeader("Accept");
disposition = accept != null && HttpUtils.accepts(accept, contentType) ? "inline" : "attachment";
}
System.out.println("Content-Type : {" + contentType + "}");
// Initialize response.
response.reset();
response.setBufferSize(DEFAULT_BUFFER_SIZE);
response.setHeader("Content-Type", contentType);
response.setHeader("Content-Disposition", disposition + ";filename=\"" + fileName + "\"");
System.out.println("Content-Disposition : {" + disposition + "}");
response.setHeader("Accept-Ranges", "bytes");
response.setHeader("ETag", fileName);
response.setDateHeader("Last-Modified", lastModified);
response.setDateHeader("Expires", System.currentTimeMillis() + DEFAULT_EXPIRE_TIME);
// Send requested file (part(s)) to client ------------------------------------------------
// Prepare streams.
try (InputStream input = new BufferedInputStream(Files.newInputStream(filepath));
OutputStream output = response.getOutputStream()) {
if (ranges.isEmpty() || ranges.get(0) == full) {
// Return full file.
System.out.println("Return full file");
response.setContentType(contentType);
response.setHeader("Content-Range", "bytes " + full.start + "-" + full.end + "/" + full.total);
response.setHeader("Content-Length", String.valueOf(full.length));
Range.copy(input, output, length, full.start, full.length);
} else if (ranges.size() == 1) {
// Return single part of file.
Range r = ranges.get(0);
System.out.println("Return 1 part of file : from ({" + r.start + "}) to ({" + r.end + "})");
response.setContentType(contentType);
response.setHeader("Content-Range", "bytes " + r.start + "-" + r.end + "/" + r.total);
response.setHeader("Content-Length", String.valueOf(r.length));
response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT); // 206.
// Copy single part range.
Range.copy(input, output, length, r.start, r.length);
} else {
// Return multiple parts of file.
response.setContentType("multipart/byteranges; boundary=" + MULTIPART_BOUNDARY);
response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT); // 206.
// Cast back to ServletOutputStream to get the easy println methods.
ServletOutputStream sos = (ServletOutputStream) output;
// Copy multi part range.
for (Range r : ranges) {
System.out.println("Return multi part of file : from ({" + r.start + "}) to ({" + r.end + "})");
// Add multipart boundary and header fields for every range.
sos.println();
sos.println("--" + MULTIPART_BOUNDARY);
sos.println("Content-Type: " + contentType);
sos.println("Content-Range: bytes " + r.start + "-" + r.end + "/" + r.total);
// Copy single part range of multi part range.
Range.copy(input, output, length, r.start, r.length);
}
// End with multipart boundary.
sos.println();
sos.println("--" + MULTIPART_BOUNDARY + "--");
}
}
}
private static class Range {
long start;
long end;
long length;
long total;
/**
* Construct a byte range.
*
* #param start Start of the byte range.
* #param end End of the byte range.
* #param total Total length of the byte source.
*/
public Range(long start, long end, long total) {
this.start = start;
this.end = end;
this.length = end - start + 1;
this.total = total;
}
public static long sublong(String value, int beginIndex, int endIndex) {
String substring = value.substring(beginIndex, endIndex);
return (substring.length() > 0) ? Long.parseLong(substring) : -1;
}
private static void copy(InputStream input, OutputStream output, long inputSize, long start, long length) throws IOException {
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int read;
if (inputSize == length) {
// Write full range.
while ((read = input.read(buffer)) > 0) {
output.write(buffer, 0, read);
output.flush();
}
} else {
input.skip(start);
long toRead = length;
while ((read = input.read(buffer)) > 0) {
if ((toRead -= read) > 0) {
output.write(buffer, 0, read);
output.flush();
} else {
output.write(buffer, 0, (int) toRead + read);
output.flush();
break;
}
}
}
}
}
private static class HttpUtils {
/**
* Returns true if the given accept header accepts the given value.
*
* #param acceptHeader The accept header.
* #param toAccept The value to be accepted.
* #return True if the given accept header accepts the given value.
*/
public static boolean accepts(String acceptHeader, String toAccept) {
String[] acceptValues = acceptHeader.split("\\s*(,|;)\\s*");
Arrays.sort(acceptValues);
return Arrays.binarySearch(acceptValues, toAccept) > -1
|| Arrays.binarySearch(acceptValues, toAccept.replaceAll("/.*$", "/*")) > -1
|| Arrays.binarySearch(acceptValues, "*/*") > -1;
}
/**
* Returns true if the given match header matches the given value.
*
* #param matchHeader The match header.
* #param toMatch The value to be matched.
* #return True if the given match header matches the given value.
*/
public static boolean matches(String matchHeader, String toMatch) {
String[] matchValues = matchHeader.split("\\s*,\\s*");
Arrays.sort(matchValues);
return Arrays.binarySearch(matchValues, toMatch) > -1
|| Arrays.binarySearch(matchValues, "*") > -1;
}
} }
use is in your controller and make sure you are not use #RestController
you should use #Controller, and following class in void method
#RequestMapping(method = RequestMethod.GET, value = "/getFile")
public void getFile(HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws Exception {
MultipartFileSender.fromPath(Paths.get("D:\8561523973120206.mp4"))
.with(httpRequest)
.with(httpResponse)
.serveResource();
}

Error Breeze OData - Metadata query failed for http://localhost:5781/odata/$metadata

I researched questions on forum but not find true result.
Error:
Metadata query failed for //localhost:5781/odata/$metadata; Unable to process returned
metadata: NamingConvention for this server property name does not roundtrip
properly:diagram_id-->Diagram_id Error: Metadata query failed for //localhost:5781/odata/$metadata; Unable to process returned metadata: NamingConvention for this server property name does not roundtrip properly:diagram_id
Code
(function () {
'use strict';
var serviceId = 'entityManagerFactory';
angular.module('myApp')
.factory(serviceId, ['breeze', emFactory]);
function emFactory(breeze) {
configureBreeze();
var serviceRoot = window.location.protocol + '//' + window.location.host + '/';
var serviceName = serviceRoot + 'odata/';
var factory = {
newManager: newManager,
serviceName: serviceName
};
return factory;
function configureBreeze() {
// use Web API OData to query and save
breeze.config.initializeAdapterInstance('dataService', 'webApiOData', true);
// convert between server-side PascalCase and client-side camelCase
breeze.NamingConvention.camelCase.setAsDefault();
}
function newManager() {
var mgr = new breeze.EntityManager(serviceName);
return mgr;
}
}})();
Code other :
(function () {
'use strict';
var serviceId = 'datacontext';
angular.module('myApp')
.factory(serviceId, ['$q', 'logger', 'entityManagerFactory', datacontext]);
function datacontext($q,logger,emFactory) {
logger = logger.forSource(serviceId);
var logError = logger.logError;
var logSuccess = logger.logSuccess;
var logWarning = logger.logWarning;
var manager = emFactory.newManager();
var service = {
getEmployees: getEmployees
};
return service;
/*Hiện thực ở đây*/
function getChangesCount(){
return manager.getChanges().length;
}
function getEmployees(forceRefresh) {
var count;
if (forceRefresh) {
if(manager.hasChanges()){
count = getChangesCount();
manager.rejectChanges();//undo tất cả các thay đổi ko được lưu
logWarning('Số nhân viên' + count + 'bị thay đổi', null, true);
}
}
// Lúc ko có forceRefesh,xem xét nhận bộ nhớ cache hơn từ xa
return breeze.EntityQuery.from('Employees')
.using(manager).execute()
.then(success).catch(failed);
function success(response) {
count = response.results.length;
logSuccess('Đã nhận ' + count + ' nhân viên', response, true);
return response.results;
}
function failed(error) {
var message = error.message || "Truy vấn để bảng nhân viên bị lỗi";
logError(message, error, true);
}
}
}})();
Code other :
(function () {
'use strict';
var controllerId = 'employees';
angular.module('myApp')
.controller(controllerId, ['datacontext', 'logger', employees]);
function employees(datacontext, logger) {
logger = logger.forSource(controllerId);
var logError = logger.logError;
var logSuccess = logger.logSuccess;
var vm = this;
vm.employees = [];
initialize();
/*Hiện thực*/
function initialize() {
getEmployees();
}
function getEmployees(forceRefresh) {
return datacontext.getEmployees(forceRefresh).then(function (data) {
return vm.employees = data;
console.log(data);
});
}
}}());
This problem very likely has to do with the camelCase naming convention and the language and/or property names that you are using. My guess is that if you remove the line that sets camelCase as the default then the error will go away. If so, then you will need to write your own custom naming convention. See http://www.breezejs.com/documentation/naming-convention
The reason that this is occurring, (I'm guessing here), is that the camelCase naming convention is very simplistic and may not work for your property names and/or language. It assumes that all server property names begin with an uppercase character and that this character can be converted to a lowercase character, and further that this process can be reversed. My guess is that one of your property names already has a first character that is lower case or that calling toLower/toUpper on some first character in a property name does not itself roundtrip. (This can occur is some non-latin character sets).
If either of these cases is occuring, its actually rather easy to create your own namingConvention to use instead of 'camelCase'. Again see the docs mentioned above.

Issues with Crypto.generateMac() in SalesForce APEX

We need to make a few callouts to a service that is using OAuth 1.0 and requires each request to be signed with HMAC-SHA1.
The service doesn't have any APEX client API. Thus, we have to do it manually.
Unfortunately,
EncodingUtil.base64Encode(Crypto.generateMac('hmacSHA1', Blob.valueOf(data), Blob.valueOf(key)));
returns a different string from what we expect. We have compared the output for the same input with libraries for other languages. And the output was different.
I have no problems calling out to OAuth 1.0. Here's some sample Apex for signing your request:
EDIT: Added additional code
private Map<String,String> getUrlParams(String value)
{
Map<String,String> res = new Map<String,String>();
if(value==null || value=='')
{
return res;
}
for(String s : value.split('&'))
{
List<String> kv = s.split('=');
if(kv.size()>1)
{
res.put(kv[0],kv[1]);
}
}
return res;
}
private String createBaseString(Map<String,String> oauthParams, HttpRequest req)
{
Map<String,String> p = oauthParams.clone();
if(req.getMethod().equalsIgnoreCase('post') && req.getBody()!=null && req.getHeader('Content-Type')=='application/x-www-form-urlencoded')
p.putAll(getUrlParams(req.getBody()));
String host = req.getEndpoint();
Integer n = host.indexOf('?');
if(n > -1)
{
p.putAll(getUrlParams(host.substring(n+1)));
host = host.substring(0,n);
}
List<String> keys = new List<String>();
keys.addAll(p.keySet());
keys.sort();
String s = keys.get(0)+'='+p.get(keys.get(0));
for(Integer i=1; i<keys.size(); i++)
s = s + '&' + keys.get(i) + '=' + p.get(keys.get(i));
return req.getMethod().toUpperCase() + '&' + EncodingUtil.urlEncode(host, 'UTF-8') + '&' + EncodingUtil.urlEncode(s, 'UTF-8');
}
public void sign(HttpRequest req)
{
nonce = String.valueOf(Crypto.getRandomLong());
timestamp = String.valueOf(DateTime.now().getTime() / 1000);
refreshParameters();
String s = createBaseString(parameters, req);
Blob sig = Crypto.generateMac('HmacSHA1', Blob.valueOf(s),
Blob.valueOf(consumerSecret+'&'+ (tokenSecret!=null ? tokenSecret : '')));
signature = EncodingUtil.urlEncode(EncodingUtil.base64encode(sig), 'UTF-8');
String header = 'OAuth ';
for (String key : parameters.keySet())
{
header = header + key + '="'+parameters.get(key)+'", ';
}
header = header + 'oauth_signature="'+signature+'"';
req.setHeader('Authorization',header);
}
This might be reaching, but could there be a case-sensitivity issue? Notice I'm calling 'HmacSHA1' not 'hmacSHA1'

Resources