debug in localhost using deployed database - google-app-engine

I use Google Eclipse Plugin, develop a web page using GWT + GAE/Java.
I would like to debug in localhost with the local GWT code but want to use the database in my actual deployed web page.
Is it possible?

Yes, you can connect to your production database using Google App Engine Remote API. You can get inspired by Gealyk Remote Connector which is doing exactly the same. Check the Remote Connector Filter source code for example. Basically you need to create filter which
Check if the Remote API is installed and if it's not install it using given credentials
Proceed to your application code using chain.doFilter(request, response)
Uninstall the Remote API Installer when the execution is finished
Just be careful that depending on your internet connection the application might be slow and sometimes timeouts.

Filter doesn't work well with RemoteServiceServlets so add these to your RemoteServiceServlet:
RemoteApiOptions options;
RemoteApiInstaller installer;
#Override
protected void onBeforeRequestDeserialized(String serializedRequest) {
if (getThreadLocalRequest().getRequestURL().indexOf("127.0.0.1") != -1) {
if (options == null) {
options = new RemoteApiOptions().server("example.appspot.com", 443).credentials("username",
"password");
installer = new RemoteApiInstaller();
try {
installer.install(options);
options.reuseCredentials("username", installer.serializeCredentials());
}
catch (IOException e) {
e.printStackTrace();
}
}
else {
installer = new RemoteApiInstaller();
try {
installer.install(options);
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
#Override
protected void onAfterResponseSerialized(String serializedResponse) {
if (getThreadLocalRequest().getRequestURL().indexOf("127.0.0.1") != -1)
installer.uninstall();
}

Related

How to connect GCP Cloud Run Java Spring to Cloud SQL SQL Server

I have a little problem, I want to deploy a Java Spring App to Cloud RUN and take connection from CLOUD SQL SQL SERVER , I know that can connect via unix socket for MySQL and Postgresql (https://cloud.google.com/sql/docs/mysql/connect-run?hl=es-419) but for SQL Server do not has the drivers jet.
Another way is to connect for Proxy Like (https://medium.com/#petomalina/connecting-to-cloud-sql-from-cloud-run-dcff2e20152a)
I tried but I can't, even that when deploy the script, it's tell me that is listenning for 127.0.0.1 for my instance id but when a tried to connect I can't.
here is my docker file
# Use the official maven/Java 8 image to create a build artifact.
# https://hub.docker.com/_/maven
FROM maven:3.5-jdk-8-alpine as builder
# Copy local code to the container image.
WORKDIR /app
COPY pom.xml .
COPY src ./src
COPY ohJpo-2.1.0.jar .
# download the cloudsql proxy binary
# RUN wget https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64 -O ./build/cloud_sql_proxy
# RUN chmod +x ./build/cloud_sql_proxy
COPY cloud_sql_proxy /build/cloud_sql_proxy
RUN chmod +x /build/cloud_sql_proxy
# copy the wrapper script and credentials
COPY run.sh /build/run.sh
COPY credentials.json /build/credentials.json
# Build a release artifact.
RUN mvn install:install-file -Dfile=/app/ohJpo-2.1.0.jar -DgroupId=ovenfo -DartifactId=ohJpo -Dversion=2.1.0 -Dpackaging=jar
RUN mvn package -DskipTests
# Use AdoptOpenJDK for base image.
# It's important to use OpenJDK 8u191 or above that has container support enabled.
# https://hub.docker.com/r/adoptopenjdk/openjdk8
# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
FROM adoptopenjdk/openjdk8:jdk8u202-b08-alpine-slim
RUN /build/cloud_sql_proxy -instances=idInstanceID=tcp:1433 -credential_file=/build/credentials.json & sleep 10
COPY --from=builder /app/target/helloworld-*.jar /helloworld.jar
# Run the web service on container startup.
CMD ["java","-Djava.security.egd=file:/dev/./urandom","-Dserver.port=${PORT}","-jar","/helloworld.jar"]
I my java application I have this way to connect, in local PC with proxy found wit
#GetMapping("/pruebacuatro")
String pruebacuatro() {
Map<String, String> config = new HashMap<String, String>();
config.put("type", "SQLSERVER");
config.put("url", "127.0.0.1");
config.put("db", "bd");
config.put("username", "user");
config.put("password", "pass");
Object data = null;
Jpo miJpo = null;
try {
miJpo = new Jpo(config);
Procedure store = miJpo.procedure("seg.menu_configuraciones");
data = store.ejecutar();
} catch (Exception e) {
e.printStackTrace();
} finally {
if(miJpo != null) {
try {
miJpo.finalizar();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return "Contents json: "+(new Gson().toJson(data));
}
I want to connect with my public IP or Private IP from my SQL Server But also I can't find information about that, Do you have any suggestion?
Cloud SQL proxy work in 2 modes: Unix socket and TCP
When you use it on your computer, you should use the TCP mode and you can connect to it in the localhost IP. However, with Cloud Run, it's the unix socket mode which is used and there isn't SQL server client that can use this connexion mode.
Thus, you have to use the Cloud SQL IP to connect your Cloud SQL instance to your Cloud Run.
For your local tests, continue to use Cloud SQL proxy in TCP mode
For Cloud Run, I recommend you to use the private IP of your SQL server.
Expose your instance in your VPC
Create a serverless VPC connector in the correct region
Attach the Serverless VPC connector to your Cloud Run service
Use the Cloud SQL private IP in your code to connect your DB.

Unable to connect a Run A Script in Selenium using IE Driver

Trying to Run a Script in Luna Eclipse using IE Driver
and getting the following error:
Exception in thread "main" org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure. Here is the code trying to Run:
package com.newpack;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
public class SecondClass {
public static void main(String[] args)
{
DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
capabilities.setCapability("requireWindowFocus", true);
capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
capabilities.setCapability(InternetExplorerDriver.INITIAL_BROWSER_URL,"https://www.google.co.in");
System.setProperty("webdriver.ie.driver","M:\\workspace\\IEDriver.exe");
WebDriver driver ;
driver = new InternetExplorerDriver(capabilities);
String baseUrl = "https://www.google.co.in";
driver.get(baseUrl);
String expectedTitle = "Google";
String actualTitle = "";
actualTitle = driver.getTitle();
if (actualTitle.contentEquals(expectedTitle)){
System.out.println("Test Passed!");
} else {
System.out.println("Test Failed");
}
driver.close();
System.exit(0);
}
}
try once following steps
Got to
internet options>>Security>>
click on:"Reset all zones to default level"
ensure that the 'Enable protected mode' checkbox is ticked for all the four zones. Like i.e Internet, Local internet, Trusted Sites, Restricted Sites.
Internet Explorer needs to be configured first in order to successfully run a selenium script.
Make sure you complete all the following steps:
Go to Internet Options > Security and enable "Protected Mode" for all the 4 zones: Internet, Local internet, Trusted Sites, Restricted Sites.
Also, click on Custom Level for all 4 zones and reset Recent Custom Settings to same level, say "Medium" or "High".
If this does not work, please update your IEDriverServer from Selenium Official Downloads

Nancy Empty Self Hosting - System.AccessViolationException

I am using Visual Studio 2015 Community Edition in Administrator mode. I installed SideWaffle Template Pack v1.21.400. I created a new Nancy Empty Self Host Project. The below is the Source Code:
using System;
using Nancy.Hosting.Self;
namespace NancyEmptySelfHost
{
class Program
{
static void Main(string[] args)
{
var uri =
new Uri("http://localhost:3579");
using (var host = new NancyHost(uri))
{
host.Start();
Console.WriteLine("Your application is running on " + uri);
Console.WriteLine("Press any [Enter] to close the host.");
Console.ReadLine();
}
}
}
}
On execution, I am getting System.AccessViolationException in the line host.Start(); with the below message:
Attempted to read or write protected memory. This is often an
indication that other memory is corrupt.
I get the same exception, when I create Projects with other Nancy Templates. If I create a Console application (without using Nancy template) and refer Nancy dlls, it's working fine. How should this be fixed?

Unable to Open IE Browser with Selenium - "Unexpected Error: Session not found"

I am trying to Open IE using webDriver and getting following error:
Unexpected error launching Internet Explorer. Protected Mode settings
are not the same for all zones. Enable Protected Mode must be set to
the same value
I have checked the Security Protected mode on for IE as suggested by other threads on the same matter. Also Zoom level is set to 100%
Still having the same issue.
Following is my code snippet:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class Browser {
public static WebDriver driver = null;
public static void main(String[] args) {
Driver("IE");
driver.get("http://google.co.uk");
driver.quit();
}
public static WebDriver Driver(String Driver_Type){
if(Driver_Type.equalsIgnoreCase("FF")){
driver = new FirefoxDriver();
}
else if(Driver_Type.equalsIgnoreCase("Chrome")){
System.setProperty("webdriver.chrome.driver", "D:\\Selenium Stuff\\Browser drivers\\chromedriver.exe" );
driver = new ChromeDriver();
}
else if(Driver_Type.equalsIgnoreCase("IE")){
System.setProperty("webdriver.ie.driver", "D:\\Selenium Stuff\\Browser drivers\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
}
return driver;
}
}
Could you kindly help me on this??
So after you change the protection setting and zoom level, it's still the same error popped out? or it becomes a different error? If you're using IE9, you may need to check this link eselenium-ieserverdriver-not-finding-new-windows-for-ie9 and it turns out enabling the compatibility mode solves the problem.
This is a very frequently asked question on the forum.
The solution is to disable the protected mode in the Internet explorer settings.
Steps to disable the protected mode:
Open Internet Explorer -> Settings -> Security Tab
There you will find 4 categories (Internet, Local Intranet, Trusted Sites, Restricted Sites)
Click on one of them to find the "Check Box" Enable Protected Mode. Make sure the field is "Unchecked".
Make sure that Protected mode is all the zones.
This will solve your issue.
More Details can be found on https://code.google.com/p/selenium/issues/detail?id=1795

HTTP Status 500 - Java Runtime Environment (JRE) version 1.7 is not supported by this driver

I am trying to access MS SQL server 2005 from a servlet file. I am using JDBC 4.0 driver.
I have already added the JAR files sqljdbc.jar and sqljdbc4.jar files to my Tomcat /lib folder.
But while running code I am getting an error
HTTP Status 500 - Java Runtime Environment (JRE) version 1.7 is not supported by this driver. Use the sqljdbc4.jar class library, which provides support for JDBC 4.0.
How is this caused and how can I solve it?
My code is:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = conn = DriverManager.getConnection("jdbc:sqlserver://localhost;databaseName=school;user=sa;password=123");
PrintWriter pwOut = res.getWriter();
pwOut.println("Connected");
Statement st = conn.createStatement();
String searchCriteria = req.getParameter("txtSearch");
ResultSet rs = st.executeQuery("select * from student");
res.setContentType("text/html");
The error message is pretty clear. Tomcat is using the wrong driver.
You state that you copied sqljdbc.jar and sqljdbc4.jar into the Tomcat lib folder. That is most probably the reason for your problem.
You only need sqljdbc4.jar otherwise Tomcat picks up the wrong one.
Try to delete sqljdbc.jar from the Tomcat lib folder
Here is my Code to connect java to Microsoft sql Server 2012
You only need sqljdbc4.jar that avail on offical Microsoft website. Here is the link:
http://download.microsoft.com/download/0/2/A/02AAE597-3865-456C-AE7F-613F99F850A8/sqljdbc_4.0.2206.100_enu.exe
It contains 2 jar files, and I am trying to use sqljdbc4.jar. This is the code I am using to connect:
package com.Sql.ConnectDB;
import java.sql.*;
public class DbClass {
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
**String url="jdbc:sqlserver://localhost;databaseName=Student";**//important
String user="username";
String pass="password";
**Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");**//important
Connection con=DriverManager.getConnection(url,user,pass);
System.out.println("Conneccted Successfully");
}catch(Exception e){
e.printStackTrace();
}
}
}

Resources