So I'm trying to take the source of the gv4me app, and modify it. I have this filter I have to get around, and the way to get past it is to put a certain keyword(lets say it's "foobar") in whatever URL you're trying to access. So I've got the source compiled and such, but I can't figure out how to add this word in properly. So far, adding it to the post doesn't work. These are the URLs that gv4me uses:
private static final String rnrURL = "https://www.google.com/voice/m/i/voicemail?p=1000";
private static final String clientLoginURL = "https://www.google.com/accounts/ClientLogin";
private final String callURL = "https://www.google.com/voice/call/connect";
private static final String markReadURL = "https://www.google.com/voice/m/mark?p=1&label=unread&id=";
private static final String getMsgsURL = "https://www.google.com/voice/inbox/recent/unread";
private static final String textURL = "https://www.google.com/voice/sms/send";
private static final String replyURL = "https://www.google.com/voice/m/sendsms";
I need to modify each of these URLs so they have "foobar" in them, but still link to the same page. Is this possible?
EDIT: Also, if you search for HandlerUI on google, you'll find a plethora of applications(closed and open source) modified to include a user interface for automatically modifying all connection attempts of said applications. However, the creator is fairly difficult to locate, so I was wondering if anyone knew how to do this?
EDIT2: It seems that adding a query string variable doesn't seem to work. What I think would most likely work, is to somehow replace www.google.com with foobar.freednsredirectservice.com. Does anyone know of anything similar to this? Something that would allow foobar.freednsredirectservice.com/voice to still work?
Try adding a query string parameter. This may not, but usually will work.
For example:
http://www.example.com/ -> http://www.example.com/?foobar
http://www.example.com/page?existing=value -> http://www.example.com/page?foobar&existing=value
Your question is unclear as to where the foobar should go, but let's assume it's after the host name, ie
"https://www.google.com/voice/sms/send" --> "https://www.google.com/foobar/voice/sms/send"
Use this:
String url = "https://www.google.com/voice/sms/send";
url = url.replaceFirst("(?<=[^:/]/)", "foobar/");
System.out.println(url); // https://www.google.com/foobar/voice/sms/send
If the foobar replaces the hostname, ie
"https://www.google.com/voice/sms/send" --> "https://foobar/voice/sms/send" use this:
Use this:
String url = "https://www.google.com/voice/sms/send";
url = url.replaceAll("//.*?/", "//foobar/");
System.out.println(url); // https://foobar/voice/sms/send
I would say no because then it would be a link is an adress and a different adress leads to a different place. What you could do is something like this:
String[] url = new String[2];
url[0] = "http://www.yoururl.com/";// Accual url
url[1] = url[0].replaceFirst("(?<=[^:/]/)", "foobar/");// For other purpose such as diplay
Related
I am working on a project, where users can write other users mails. The mail-class looks currently like this:
public class Mail
{
/*
* Fields
*/
private final String _content;
private final String _sender;
private final String _date;
private final String _topic;
/**
* Constructor of the Mail-class.
*
* #param mail The mail as a string representation.
*/
public Mail(String mail)
{
_content = ...;
_sender = ...;
_date = ...;
_topic = ...;
}
// And all the getters.
#Override
public String toString()
{
// Make the reperentation as a String
}
Now, the problem is, that I need to save all the mails into a .yml userdata file. For me, the simplest way to do this is to firstly convert every mail to a String and then save all mails into a List<String> mails which will be written directly into the .yml where I can access and read the list of String with ease. Now, once I start the server, all mails should be converted back into the object-representation. The question is, how can I save the mail-Object as a String and load it, that I can operate using those mail objects.
My first ideas were something like this sender#date#topic#content and then splitting the String via String.split("#");. But when I do this, I need to ban the # character for the content and the topic. The date and the username will never contain #. I don't want to ban any character. Unfortunately I have nothing found on my researches so far on how to solve this task.
The best thing to do is use XML or JSON to markup the object and convert it to a string for you. There are tons of libraries you can use to do that. They all operate in roughly the same manner: add an annotations to your fields and class.
I would like to know how could I add more than one prefix to my discord bot?
This is my current prefix listener code, the prefix is being pulled from a .env
final long guildId = event.getGuild().getIdLong();
String prefix = Config.get("PREFIX");
String raw = event.getMessage().getContentRaw();
if (raw.startsWith(prefix)) {
manager.handle(event, prefix);
}
Currently my bot's prefix is based on mention <#!ID>, but this type of mention doesn't work on mobile discord (mobile discord uses only <#ID> for mention, without the "!"), so I would like to add both variants to be used on my bot.
I was told I could use regex for that but I have no clue how it works and how to apply it to my code.
Just define a second prefix and check, if the message starts with one of them.
final long guildId = event.getGuild().getIdLong();
String prefix1 = Config.get("PREFIX1");
String prefix2 = Config.get("PREFIX2");
String raw = event.getMessage().getContentRaw();
if (raw.startsWith(prefix1) || raw.startsWith(prefix2) {
//...
}
Situation: I'm working on legacy code and trying to improve readability. The following example should visualize the intent:
private static final String CONSTANT_1 = "anyValue";
private static final String CONSTANT_2 = "anyValue";
private static final String CONSTANT_3 = "anyValue";
private static final String CONSTANT_4 = "anyValue";
private static final String CONSTANT_5 = "anyValue";
private final SomeType someField = new SomeType();
private void contentOfSomeMethods(){
someMethod(someField, CONSTANT_1, true);
someMethod(someField, CONSTANT_2, true);
someMethod(someField, CONSTANT_3, true);
someMethod(someField, CONSTANT_4, false);
someMethod(someField, CONSTANT_5, false);
}
private void someMethod(SomeType type, String value, boolean someFlag) { }
Imagine, there are about 50 calls of someMethod using about 50 constants. I want to do safe automatical refactorings on that code so that the contentOfSomeMethods method changes to
private void contentOfSomeMethods(){
doItWith(CONSTANT_1);
doItWith(CONSTANT_2);
doItWith(CONSTANT_3);
doItNotWith(CONSTANT_4);
doItNotWith(CONSTANT_5);
}
and two additional methods are generated:
private void doItWith(String value) {
someMethod(someField, value, true);
}
private void doItNotWith(String value) {
someMethod(someField, value, false);
}
The naive way is to extract all constants in contentOfSomeMethods inside local variables and use then the extract method refactoring to create the desired methods. And afterwards to inline back the local variables. But this solution doesn't scale up.
Another way is to use search and replace with regular expressions, but this is not a safe refactoring, so I could break the code without noticing it.
Do you have any better suggestions? Do you know some plugins for Eclipse that allow that?
I don't know of any utility that would do this directly.
I think using a regular expression is the only to go. First, you will need to create the two target methods doItWith and doItNotWith. Then, you can highlight the contents of the method contentOfSomeMethods, hit Ctrl+F, and use the following regular expressions:
Find: someMethod\(someField, (\w*), true\);
Replace with: doItWith(\1);
and then
Find: someMethod\(someField, (\w*), false\);
Replace with: doItNotWith(\1);
Be sure to check "Regular Expressions" and "Selected lines". Here's a picture of it:
The regular expressions match the constant that is used inside the function call with (\w*) and then it is used during the replacement with \1. Using this regular expression only on the selected lines minimizes the chance of breaking unrelated code.
Do it with a regular expression and verify it.
I'm assuming that each call to someMethod spans only one line. If not this method is still useful but slower.
Copy the original file.
Use ctrl+alt+h to show the Callers of someMethod and get a count of them.
Do regex search and replaces restricted to the proper area :
Find : someMethod(someField,([ ]*CONSTANT_[0-9]+)[ ]*,[ ]*true[ ]*)[ ]*;
Replace : doItWith("$1");
Find : someMethod(someField,([ ]*CONSTANT_[0-9]+)[ ]*,[ ]*false[ ]*)[ ]*;
Replace : doItNotWith("$1");
Make a diff of the original file and the new file showing only the lines of the original file which have changed.
diff --changed-group-format='%<' --unchanged-group-format='' original.java refactored.java | wc
You should get the same number of lines as you got in the callers of someMethod.
If the calls to someMethod are multiline, or if you want greater verification, just drop | wc to see the lines which were modified in the original file to ensure that only the correct lines have been modified.
Alas I know nothing in Eclipse that allows to do this today.
This is something I would like to achieve one day in AutoRefactor: https://github.com/JnRouvignac/AutoRefactor/issues/8
However the road to get there is quite long.
The only ways I know today are to extract local variables then extract method (as you suggested) or use regexes (as somebody else suggested).
I have query string like that:
ObjectGUId=1abcde&ObjectType=2&ObjectTitle=maximumoflife&Content=racroi&TimeStamp=2012-11-05T17:20:06.056
And I have Java Object:
LogObject{
private String ObjectGUId;
private String ObjectType;
private String ObjectTitle;
private String Content;
private String TimeStamp;
}
So i want to parse this query string to this java Object.
I've searched and read many question but not gotten correct answer yet.
Show me what can solve this problem.
Inspired by #bruno.braga, here's a way using Apache http-components. You leverage all the parsing corner cases:
List<NameValuePair> params =
URLEncodedUtils.parse("http://example.com/?" + queryString, Charset.forName("UTF-8"));
That'll give you a List of NameValuePair objects that should be easy to work with.
If you do not really need to push the querystring into your own class (you might want that though), instead of parsing it manually, you could use the URLDecoder, as #Sonrobby has commented:
String qString = "ObjectGUId=1abcde&ObjectType=2&ObjectTitle=maximumoflife";
Uri uri = Uri.parse(URLDecoder.decode("http://dummy/?" + qString, "UTF-8"));
if (uri != null) {
for(String key: uri.getQueryParameterNames()) {
System.out.println("key=[" + key + "], value=[" + uri.getQueryParameter(key) + "]");
}
}
The "dummy" looks dirty but it is required if what you only have is the querystring values (qString). If you have the complete URL, just pass it directly to the URLDecoder, and you are done.
Etiquette
You really should be much more specific about what you have tried and why it didn't work.
A proper code sample of your LogObject would really be very helpful here.
Ideally, you would provide a SSCCE so others could easily test your problem themselves.
Answer
You can extract the name:value pairs like this:
String toParse = "ObjectGUId=1abcde&ObjectType=2&ObjectTitle=maximumoflife&Content=racroi&TimeStamp=2012-11-05T17:20:06.056";
String[] fields = toParse.split("&");
String[] kv;
HashMap<String, String> things = new HashMap<String, String>();
for (int i = 0; i < fields.length; ++i)
{
t = fields[i].split("=");
if (2 == kv.length)
{
things.put(kv[0], kv[1]);
}
}
I have chosen to put them into a HashMap, but you could just as easily look at the name part (kv[0]) and choose to do something with it. For example:
if kv[0].equals("ObjectGUId")
{
logObject.setGUId(kv[1]); // example mutator/setter method
}
else if //...
However, all your fields in LogObject are private and you haven't shown us any methods, so I hope you have some way of setting them from outside... bear in mind you will need to store the pairs in a data structure of some kind (as I have done with a HashMap) if you intend to intialise a LogObject with all the fields rather than setting the fields after a constructor call.
Speaking of SSCCEs, I made one for this answer.
Not all that sure how I would describe this question, so I'll jump right into the example code.
I have a Constants.java
package com.t3hh4xx0r.poc;
public class Constants {
//RootzWiki Device Forum Constants
public static final String RWFORUM = "http://rootzwiki.com/forum/";
public static final String TORO = "362-cdma-galaxy-nexus-developer-forum";
public static String DEVICE;
}
In trying to determine the device type, I use this method.
public void getDevice() {
Constants.DEVICE = android.os.Build.DEVICE.toUpperCase();
String thread = Constants.(Constants.DEVICE);
}
Thats not correct though, but thats how I would think it would have worked.
Im setting the Constants.DEVICE to TORO in my case on the Galaxy Nexus. I want to then set the thread String to Constants.TORO.
I dont think I'm explaining this well, but you shoudl be able to understand what I'm trying to do fromt he example code. I want
Constants.(VALUE OF WHAT CONSTANTS.DEVICE IS) set for the String thread.
Another way to put it,
I want to get Constants.(//value of android.os.Build.DEVICE.toUpperCase())
I apologies for the poorly worded question, i dont know of any better way to explain what Im trying to achieve.
Im trying to determine the thread based on the device type. I could go in and do an
if (Constants.DEVICE.equals("TORO"){
String thread = Constants.TORO;
}
But I plan on adding a lot more device options in the future and would like to make it as easy as adding a string to the Constants.java rather than having to add another if clause.
I would suggest using an enum instead of just strings - then you can use:
String name = android.os.Build.DEVICE.toUpperCase();
// DeviceType is the new enum
DeviceType type = Enum.valueOf(DeviceType.class, name);
You can put the value of the string in a field for the enum, and expose it via a property:
public enum DeviceType {
RWFORUM("http://rootzwiki.com/forum/"),
TORO("362-cdma-galaxy-nexus-developer-forum");
private final String forumUrl;
private DeviceType(String forumUrl) {
this.forumUrl = forumUrl;
}
public String getForumUrl() {
return forumUrl;
}
}
(I'm guessing at the meaning of the string value - not a great guess, but hopefully it gives the right idea so you can make your actual code more meaningful.)
EDIT: Or to use a map:
Map<String, String> deviceToForumMap = new HashMap<String, String>();
deviceToForumMap.put("RWFORUM", "http://rootzwiki.com/forum/");
deviceToForumMap.put("TORO", "362-cdma-galaxy-nexus-developer-forum");
...
String forum = deviceToForumMap.get(android.os.Build.DEVICE.toUpperCase());
You can use reflection:
Constants.DEVICE = android.os.Build.DEVICE.toUpperCase();
String thread = (String) Constants.class.getField(Constants.DEVICE).get(null);
Not sure I've understood the question properly, but I feel that it's a right place to use a Map. The outcome will be something like this:
HashMap<String, String> map;
map.put(TORO, DEVICE);
Constants.DEVICE = android.os.Build.DEVICE.toUpperCase();
String thread = map.get(Constants.DEVICE);
Sorry for a possible misunderstanding or your question, but I hope you've got the idea.
P.S. You can find more info about Maps in the Java documentation: Map, HashMap.